A method, device, equipment and medium for locating heap memory out-of-bounds
By locating and detecting heap memory out-of-bounds events that occur in embedded devices, only the involved memory areas are detected, which solves the problems of resource waste and performance degradation caused by detecting all heap memories in the existing technology, and is suitable for embedded devices with limited memory. equipment.
Patent Information
- Application Number
- CN202411021433.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-26
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2044-07-26
AI Technical Summary
Existing dynamic memory detection tools will detect all heap memory when performing heap memory detection, resulting in performance degradation of resource-constrained embedded devices, and may even cause memory overflow (OOM) problems.
By locating the first memory area where heap memory out of bounds occurs, and only detecting this area to avoid detecting all heap memory, the preset memory application function is used to apply for memory in a page-aligned manner, and the memory area to be monitored is recorded and modified. status, execute each instruction in the application to determine whether a heap memory out-of-bounds occurrence has occurred.
It saves additional memory resources and reduces the performance impact on the process. It is suitable for embedded devices with small memory.
Smart Images

Figure CN118550755B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a heap memory cross-boundary positioning method, device, equipment and medium. Background Art
[0002] With the development of science and technology, the application scenarios of embedded devices are increasing, the complexity of embedded applications and embedded systems is increasing, and limited hardware resources and increasingly complex functional requirements have become a sharp contradiction in the field of embedded development. At the same time, due to the stringent requirements of application scenarios, embedded software must run stably and efficiently, which places high demands on the quality of embedded software. Embedded software debugging is very difficult, especially some occasional heap memory out-of-bounds problems, which are very time-consuming to debug and locate.
[0003] In the prior art, the commonly used heap memory cross-bounds location method can only locate the code position when the application cannot continue to run after the heap memory cross-bounds occurs. However, since the heap memory is globally accessible, the culprit of the heap memory cross-bounds may have been hidden. In order to determine the code that has illegal memory access when the heap memory cross-bounds occurs, some dynamic memory detection tools can be used to perform memory detection when the heap memory cross-bounds occurs, such as Valgrind tools, Sanitizer tools, etc. However, these tools are based on source code plugging, adding protection byte combinations or establishing mapping tables to collect and analyze data. When detecting, these tools will not only apply for all the memory corresponding to the application runtime, but also apply for additional memory to record memory information. Since the above-mentioned dynamic memory detection tools will detect all heap memories during detection, a large amount of additional memory is consumed during detection. For embedded platforms with scarce resources, these tools will affect the performance of embedded software, and even cause the phenomenon of embedded software memory overflow (Out Of Memory, OOM).
[0004] In summary, existing dynamic memory detection tools will detect all heap memories when performing heap memory detection, which consumes a lot of additional memory and is not suitable for embedded devices with small memory. Summary of the invention
[0005] The embodiments of the present application provide a heap memory out-of-bounds location method, apparatus, device and medium to solve the problem in the prior art that the dynamic memory detection tool will detect all heap memories when performing heap memory detection, which requires a large amount of additional memory and is not suitable for embedded devices with smaller memory.
[0006] In a first aspect, an embodiment of the present application provides a method for locating heap memory out-of-bounds, the method comprising:
[0007] When a heap memory cross-border occurs, locate the first heap memory area where the heap memory cross-border occurs, and determine the first source code for applying for the first heap memory area;
[0008] Based on the first heap memory area, determining a memory area to be monitored applied for by the first source code, wherein the memory area to be monitored includes the first heap memory area;
[0009] Execute each instruction in the application program. If it is recognized that the executed instruction accesses the memory area to be monitored, for each instruction that accesses the memory area to be monitored, determine whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored according to the storage location of the corresponding data recorded when the instruction is executed. If not, it is determined that a heap memory out of bounds occurs when executing the instruction, and the location of the recorded code of the instruction in the application program is determined.
[0010] In a second aspect, an embodiment of the present application further provides a heap memory cross-bounds location device, the device comprising:
[0011] A determination module, used for locating a first heap memory area where the heap memory crosses the boundary when the heap memory crosses the boundary, and determining a first source code for applying for the first heap memory area;
[0012] An application module, configured to determine, based on the first heap memory area, a memory area to be monitored applied for the first source code, wherein the memory area to be monitored includes the first heap memory area;
[0013] The positioning module is used to execute each instruction in the application program. If it is identified that the executed instruction accesses the memory area to be monitored, for each instruction that accesses the memory area to be monitored, according to the storage location of the corresponding data recorded when the instruction is executed, it is determined whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored. If not, it is determined that a heap memory out of bounds occurs when the instruction is executed, and the location of the recorded code of the instruction in the application program is determined.
[0014] In a third aspect, an embodiment of the present application further provides an electronic device, which includes at least a processor and a memory, and the processor is used to implement the steps of the heap memory out-of-bounds location method as described in any one of the above items when executing a computer program stored in the memory.
[0015] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the heap memory out-of-bounds location method as described in any one of the above items.
[0016] Because in the embodiment of the present application, when a heap memory cross-border occurs, the first heap memory area where the heap memory cross-border occurs is located, and the first source code that applies for the first heap memory area is determined; based on the first heap memory area, the memory area to be monitored applied for by the first source code is determined, wherein the memory area to be monitored includes the first heap memory area; each instruction in the application is executed, and if it is identified that the executed instruction accesses the memory area to be monitored, for each instruction that accesses the memory area to be monitored, according to the storage location of the corresponding data recorded when the instruction is executed, it is determined whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored; if not, it is determined that a heap memory cross-border occurs when the instruction is executed, and the position of the recorded code of the instruction in the application is determined. The embodiment of the present application only detects the memory area to be monitored corresponding to the first source code where the heap memory cross-border occurs, and there is no need to detect all heap memories, which saves additional memory resources and reduces the performance impact on the process, and can be suitable for embedded devices with smaller memory. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0018] Figure 1 A schematic diagram of a heap memory out-of-bounds location process provided in an embodiment of the present application;
[0019] Figure 2 A schematic diagram of a positioning module provided in an embodiment of the present application;
[0020] Figure 3 A schematic diagram of an instruction execution flow in an application provided in an embodiment of the present application;
[0021] Figure 4 One of the schematic diagrams of a heap memory out-of-bounds location process provided in an embodiment of the present application;
[0022] Figure 5 A second schematic diagram of a heap memory out-of-bounds location process provided in an embodiment of the present application;
[0023] Figure 6 A schematic diagram of the structure of a heap memory cross-boundary positioning device provided in an embodiment of the present application;
[0024] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0025] In order to make the purpose and implementation method of the present application clearer, the exemplary implementation method of the present application will be clearly and completely described below in conjunction with the drawings in the exemplary embodiments of the present application. Obviously, the described exemplary embodiments are only part of the embodiments of the present application, rather than all the embodiments.
[0026] It should be noted that the brief description of terms in this application is only for the convenience of understanding the embodiments described below, and is not intended to limit the embodiments of this application. Unless otherwise specified, these terms should be understood according to their common and usual meanings.
[0027] The terms "first", "second", "third", etc. in the specification and claims of this application and the above drawings are used to distinguish similar or similar objects or entities, and do not necessarily mean to limit a specific order or sequence, unless otherwise noted. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances.
[0028] The terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover but not exclude inclusion, for example, a product or device comprising a list of components is not necessarily limited to all the components expressly listed but may include other components not expressly listed or inherent to such product or device.
[0029] The term "module" refers to any known or later developed hardware, software, firmware, artificial intelligence, fuzzy logic, or combination of hardware and / or software code that is capable of performing the functions associated with that element.
[0030] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
[0031] For the convenience of explanation, the above description has been made in conjunction with specific embodiments. However, the above exemplary discussion is not intended to be exhaustive or limit the embodiments to the specific forms disclosed above. Based on the above teachings, various modifications and variations can be obtained. The selection and description of the above embodiments are to better explain the principles and practical applications, so that those skilled in the art can better use the embodiments and various different variations of the embodiments suitable for specific use considerations.
[0032] The embodiment of the present application provides a method, device, equipment and medium for locating a heap memory cross-border. In the method, when a heap memory cross-border occurs in the embodiment of the present application, the first heap memory area where the heap memory cross-border occurs is located, and the first source code that applies for the first heap memory area is determined; based on the first heap memory area, the memory area to be monitored applied for by the first source code is determined, wherein the memory area to be monitored includes the first heap memory area; each instruction in the application is executed, and if it is identified that the executed instruction accesses the memory area to be monitored, for each instruction that accesses the memory area to be monitored, according to the storage location of the corresponding data recorded when the instruction is executed, it is determined whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored; if not, it is determined that a heap memory cross-border occurs when the instruction is executed, and the position of the recorded code of the instruction in the application is determined. The embodiment of the present application only detects the memory area to be monitored corresponding to the first source code where the heap memory cross-border occurs, and there is no need to detect all heap memories, thereby saving additional memory resources and reducing the performance impact on the process, and can be applicable to embedded devices with smaller memory. Embodiment 1:
[0033] Figure 1 A schematic diagram of a heap memory out-of-bounds location process provided in an embodiment of the present application, the process includes:
[0034] S101: When a heap memory boundary crossing occurs, a first heap memory area where the heap memory boundary crossing occurs is located, and a first source code for applying for the first heap memory area is determined.
[0035] The heap memory out-of-bounds location method provided in the embodiments of the present application is applied to an electronic device, which may be a personal computer (PC), a server, etc.
[0036] When an electronic device identifies that a heap memory cross-border situation occurs when executing a certain application, it first locates the first heap memory area where the heap memory cross-border situation occurs through a debugging tool, and locates the first source code that applies for the first heap memory area based on the first heap memory area. The process of locating the first heap memory area where the heap memory cross-border situation occurs and locating the first source code that applies for the first heap memory area is prior art and will not be described in detail here.
[0037] Furthermore, it is understandable that when the heap memory out-of-bounds positioning begins, the electronic device controls the application to stop running.
[0038] S102: Based on the first heap memory area, determine a memory area to be monitored applied for by the first source code, wherein the memory area to be monitored includes the first heap memory area.
[0039] In an embodiment of the present application, based on the first heap memory area corresponding to the first source code, a memory application function is used to apply for a memory area to be monitored for the first source code; wherein the memory area to be monitored includes the first heap memory area, and the memory size of the first heap memory area is smaller than the memory size of the memory area to be monitored, thereby avoiding detection of all heap memory areas, while ensuring that the first heap memory area can be detected, saving additional memory resources, and reducing the performance impact on the process.
[0040] Among them, since when the system memory application function (i.e., the original memory application function in the first source code) is used to apply for memory for the first source code, the information of the applied first heap memory area cannot be recorded, and the status of the applied first heap memory area cannot be modified, therefore, in the embodiment of the present application, the memory application function used can be a preset memory application function, which can be used to record the information of the applied memory area to be monitored after the preset memory application function is used to apply for the memory area to be monitored, and the preset memory application function applies for memory in a page-aligned manner, while in the operating system, when setting memory attributes, it is necessary to do so in units of pages, therefore, in the embodiment of the present application, after the preset memory application function is used to apply for the memory area to be monitored for the first source code, the status of the memory area to be monitored can also be modified. Among them, the preset memory application function can be a custom memory application function, which is not limited here.
[0041] Among them, the memory area to be monitored is a memory area used to store memory data generated when the relevant instructions in the first source code are running, and if the memory data generated when the relevant instructions in the first source code are running exceeds the area range of the first heap memory area contained in the memory area to be monitored when stored in the memory area to be monitored, it is determined that a heap memory cross-border occurs.
[0042] S103: Execute each instruction in the application program. If it is identified that the executed instruction accesses the memory area to be monitored, for each instruction accessing the memory area to be monitored, determine whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored according to the storage location of the corresponding data when the instruction is executed.
[0043] The electronic device starts a parent process, controls the parent process to call its child process, causes the child process to start an application, and controls the child process to execute each instruction in the application in sequence. After controlling the child process to start the application, the parent process can call a process trace (ptrace) function to track the child process, thereby facilitating the control of the operation of the child process.
[0044] Among them, in a possible implementation, since the application is recompiled after the application running when the boundary is determined to be crossed, in the embodiment of the present application, the application started by the child process can be a compiled application.
[0045] In an embodiment of the present application, after locating the memory area to be monitored of the application, in order to locate the cross-border, each instruction in the application can be executed in sequence. Each instruction in the application is executed, and it is determined whether the executed instruction accesses the memory area to be monitored. If it is determined that the executed instruction accesses the memory area to be monitored, then for each instruction that accesses the memory area to be monitored, the parent process is controlled to obtain and record the storage location of the corresponding data when the instruction is executed. In a possible implementation, after each instruction in the application is executed, according to the storage location of the corresponding data recorded when the instruction is executed, it is determined whether the data corresponding to the instruction is all written into the first heap memory area contained in the memory area to be monitored. In another possible implementation, after all instructions in the application are executed, it is determined in sequence according to the storage location of the corresponding data recorded when each instruction is executed. Whether the data corresponding to the instruction is all written into the first heap memory area. If the data corresponding to a certain instruction is all written into the first heap memory area, a judgment is made for the next instruction. If the data corresponding to the instruction is not all written into the first heap memory area, S104 is continued.
[0046] Among them, in order to obtain the storage location of the corresponding data when the instruction is executed, the ptrace (PTRACE_PEEKDATA) function is called in the parent process to obtain the data and storage location stored in the memory where the memory area to be monitored is located when the instruction is executed, that is, the data and storage location corresponding to the instruction.
[0047] In another possible implementation, in order to determine whether the data corresponding to a certain instruction is all written into the first heap memory area while determining the specific data that has changed, in an embodiment of the present application, the electronic device can also control the parent process to obtain the saved data and storage location corresponding to the previous instruction, and determine whether there is a change between the data corresponding to the instruction and the data corresponding to the previous instruction at each storage location based on the data and storage location corresponding to the instruction and the data and storage location corresponding to the previous instruction, count the changed data and storage location of the data corresponding to the instruction compared with the data corresponding to the previous instruction, mark the changed data and the corresponding storage location, and determine whether the storage location corresponding to the changed data is outside the first heap memory area included in the memory area to be monitored. If so, determine that the data corresponding to the instruction is not all written into the first heap memory area, and continue to execute S104; if not, determine that no heap memory boundary crossing occurs when executing the instruction, and continue to judge the next instruction.
[0048] S104: If data corresponding to any instruction is written outside the first heap memory area, it is determined that a heap memory cross-border occurs when executing the instruction, and the location of the recorded code of the instruction in the application is determined.
[0049] If it is determined that part or all of the data corresponding to any instruction is written outside the first heap memory area included in the memory area to be monitored, it is determined that a heap memory cross-border occurs when executing the instruction.
[0050] In addition, in order to facilitate the rapid and accurate determination of the location of the code of the instruction that caused the heap memory cross-border in the application, in the embodiment of the present application, each time an instruction is executed, the electronic device controls the parent process to obtain and record the location of the code of the instruction in the application. Therefore, when it is determined that the heap memory cross-border occurs when executing any instruction, the location of the code of each instruction recorded in the application is determined based on the location of the code of the instruction that caused the heap memory cross-border in the application.
[0051] In an embodiment of the present application, only the memory area to be monitored corresponding to the first source code where the heap memory crosses the boundary is detected, and there is no need to detect all the heap memories, which saves additional memory resources and reduces the performance impact on the process. It can be suitable for embedded devices with smaller memory. Embodiment 2:
[0052] In order to more accurately determine the memory area to be monitored, based on the above embodiment, in the embodiment of the present application, based on the first heap memory area, determining the memory area to be monitored for the first source code application includes:
[0053] The target number of pages is determined according to the number of first bytes stored in the first heap memory area, and the memory area to be monitored applied for the first source code is determined according to the target number of pages using a preset memory application function.
[0054] In an embodiment of the present application, based on the first byte quantity of memory data that can be stored in the first stack memory area originally applied for by the first source code and the second byte quantity of memory data that can be stored in each page of the memory applied for in a page-aligned manner, the integer part of the ratio of the first byte quantity to the second byte quantity is determined, and one is added to the integer part to obtain the target number of pages.
[0055] For example, assuming that the first byte quantity of memory data that can be stored in the first heap memory area is 1037, and the second byte quantity of memory data that can be stored in each page of the memory applied for in a page-aligned manner is 1024, then the integer part of the ratio of the total number of items to the number of sub-items is determined to be 1, and then one is added to the integer to determine the target number of pages to be 2. And, it can be understood that if the ratio of the first byte quantity to the second byte quantity is an integer, the integer is directly determined as the integer part of the ratio of the first byte quantity to the second byte quantity, and one is added to the integer part to obtain the target number of pages.
[0056] After obtaining the target number of pages, according to the first starting point of the first heap memory area, the control subprocess uses the preset memory application function to apply for the first source code in a page-aligned manner for a memory area to be monitored with the first starting point of the first heap memory area as the starting point and the number of pages occupied being the target number of pages. Among them, since the total number of bytes of memory data that can be stored in the target number of pages corresponding to the memory area to be monitored is greater than the first number of bytes that can be stored in the first heap memory area, in the embodiment of the present application, the memory area to be monitored includes the first heap memory area.
[0057] Among them, the preset memory request function not only applies for memory from the operating system in a page-aligned manner, but also records the requested memory information, which includes the size of the requested memory block, the requested memory address, the page code range occupied by the memory, and the actual effective length of the memory.
[0058] The actual effective length of the memory may be the actual length of the first heap memory area, and the page number range occupied by the memory may be the page number range starting from the first starting point and corresponding to the target number of pages.
[0059] In an embodiment of the present application, since the total number of bytes of memory data that can be stored in the target number of pages is greater than the first number of bytes that can be stored in the first heap memory area, it is possible to more accurately apply for the memory area to be monitored for the first source code based on the target number of pages, while ensuring that the memory area to be monitored includes the first heap memory area. Embodiment 3:
[0060] On the basis of the above embodiments, in the embodiment of the present application, before determining the memory area to be monitored as the first source code application based on the first heap memory area, the method further includes:
[0061] Using a preset memory request function to replace the original memory request function in the first source code to obtain a recompiled application program;
[0062] Execute every instruction in your application, including:
[0063] Execute each instruction in the recompiled application.
[0064] In an embodiment of the present application, in order to be able to manipulate the memory area corresponding to the first source code, before applying for the memory area to be monitored for the first source code, the initialization code of the preset positioning module is added to the first source code of the application, and the preset memory application function in the positioning module is used to replace the original memory application function contained in the first source code when the boundary is crossed, and the application is recompiled to obtain a compiled application, so as to facilitate the child process to call the preset memory application function in the compiled first source code to apply for memory for the first source code.
[0065] Specifically, the positioning module can also include multiple functions, such as Figure 2 The schematic diagram of the positioning module shown in the figure includes a module initialization function and a preset memory application function. The module initialization function is used to initialize certain setting information, such as registering a specific signal, etc. The function of the preset memory application function has been described in the above embodiment and will not be repeated here.
[0066] After obtaining the recompiled application, each instruction in the recompiled application is executed, and based on the recompiled application, the subsequent step of determining whether the executed instruction accesses the memory area to be monitored in S103 is continued. Embodiment 4:
[0067] On the basis of the above embodiments, in the embodiment of the present application, after determining the memory area to be monitored applied for the first source code, before executing each instruction in the recompiled application program, the method includes:
[0068] Set the memory where the memory area to be monitored is located to an inaccessible state.
[0069] In an embodiment of the present application, after the memory application for the first source code is completed using a preset memory application function and the memory area to be monitored corresponding to the first source code is determined, in order to facilitate the control of the operation of the recompiled application, before executing the first instruction in the recompiled application, the memory where the memory area to be monitored is located is set to an inaccessible state. Embodiment 5:
[0070] In order to make the child process execute each instruction in sequence, based on the above embodiments, in the embodiment of the present application, each instruction in the recompiled application is executed, including:
[0071] The following process is executed in the order of instructions in the recompiled application program until each instruction in the recompiled application program is executed:
[0072] For any instruction, if it is identified that the area accessed by the instruction is not a memory area to be monitored, the instruction is executed directly; if it is identified that the area accessed by the instruction is a memory area to be monitored, the instruction is executed after the memory where the memory area to be monitored is located is configured to be accessible, and the location of the instruction code in the application is recorded.
[0073] In order to ensure that only one instruction in the application is executed at a time, in an embodiment of the present application, after calling the parent process, the electronic device loops through the following process in the order of instructions in the recompiled application, so that the child process executes each instruction in sequence until the recompiled application is run to completion, wherein the recompiled application is composed of multiple instructions, and the recompiled application is run by running the instructions in the recompiled application.
[0074] For any instruction, determine whether the area accessed by the instruction is a memory area to be monitored. If it is determined that the area accessed by the instruction is not a memory area to be monitored, the electronic device controls the parent process to call its child process to directly execute the instruction and records the location information of the instruction in the recompiled application.
[0075] If it is determined that the area accessed by the instruction is the memory area to be monitored, the electronic device controls the parent process to call its child process, so that the child process accesses the memory where the memory area to be monitored is located. Since the memory where the memory area to be monitored is located is set to an inaccessible state, a segmentation fault occurs when the child process accesses the memory where the memory area to be monitored is located, and a segmentation fault (SIGSEGV) signal is sent to the parent process, and the recompiled application is controlled to stop execution. After the parent process receives the SIGSEGV signal, since the memory area to be monitored is in an inaccessible state, in order to ensure that the instructions in the recompiled application can be executed, in an embodiment of the present application, the first memory state modification function is called to modify the memory where the memory area to be monitored is located to an accessible state, and then the single-step debugging ptrace (PTRACE_SINGLESTEP) function is called to control the child process to restart the stopped recompiled application, thereby ensuring that when the child process restarts the stopped recompiled application, the recompiled application can run normally.
[0076] The first memory state modification function is one of the functions included in the positioning module (such as Figure 2 As shown), and the first memory state modification function is used to set the memory where the memory area to be monitored is located to an accessible state.
[0077] Since the location information of each instruction in the recompiled application is recorded after each instruction is executed, after the child process restarts the stopped recompiled application, it executes the next instruction of the instruction corresponding to the location according to the recorded location of the recompiled application before it was stopped, and after executing the next instruction, it controls the child process to stop the recompiled application and records the location of the recompiled application before it was stopped (that is, records the location information of the next instruction in the application), so as to facilitate the subsequent execution of the next instruction of the instruction corresponding to the location information according to the recorded location information, and modify the memory where the memory area to be monitored is located to be inaccessible.
[0078] Among them, in a possible implementation, since the ptrace (PTRACE_SINGLESTEP) function is used to enable the child process to restart the stopped recompiled application, execute the next instruction of the last stopped position in the recompiled application, and stop the recompiled application after executing the instruction, the child process can be controlled to stop the recompiled application by calling the ptrace (PTRACE_SINGLESTEP) function.
[0079] Furthermore, in order to ensure the execution of the next instruction, in an embodiment of the present application, the ptrace (PTRACE_CONT) function is called to re-run, thereby continuing to control the parent process to call its child process to execute the next instruction in the recompiled application.
[0080] In an embodiment of the present application, according to the order of instructions in the recompiled application, for each instruction that accesses the memory area not to be monitored, the instruction is directly executed, and for each instruction that accesses the memory area to be monitored, the state of the memory where the memory area to be monitored is modified and the ptrace (PTRACE_SINGLESTEP) function is used to ensure that the child process executes each instruction in sequence according to the order of each instruction in the recompiled application. Embodiment 6:
[0081] On the basis of the above embodiments, in an embodiment of the present application, if it is identified that the area accessed by the instruction is a memory area to be monitored, after executing the instruction, the following steps are included:
[0082] Change the memory where the memory area to be monitored is located to an inaccessible state.
[0083] In an embodiment of the present application, if it is identified that the area accessed by the instruction is a memory area to be monitored, then since the child process has already executed an instruction in the recompiled application when recording the running position before the recompiled application was stopped, after it is identified that the child process has executed an instruction, that is, after recording the running position before the recompiled application was stopped, the second memory state modification function is used to modify the memory where the memory area to be monitored is located to an inaccessible state, and the re-run ptrace (PTRACE_CONT) function is called, so as to continue to control the parent process to call its child process to continue running, so that the child process continues to execute the next instruction in the recompiled application, and if the memory where the memory area to be monitored is located in an inaccessible state, the memory where the memory area to be monitored is continued to be configured as an accessible state, and the ptrace (PTRACE_SINGLESTEP) function is used to control the parent process to call its child process to execute the next instruction in the recompiled application.
[0084] The second memory state modification function is one of the functions included in the positioning module (such as Figure 2 As shown), and the second memory state modification function is used to set the memory where the memory area to be monitored is located to an inaccessible state.
[0085] The above embodiments are described below with a specific embodiment. Figure 3 The schematic diagram of the instruction execution flow in the application program includes the following steps:
[0086] S301: After the memory application for the application is completed, the memory where the memory area to be monitored is located is set to an inaccessible state.
[0087] S302: Control the parent process to call its child process, so that the child process accesses the memory where the memory area to be monitored is located. The child process sends a SIGSEGV signal to the parent process. After receiving the SIGSEGV signal, the parent process modifies the memory where the memory area to be monitored is located to an accessible state.
[0088] S303: Control the parent process to call the single-step debugging ptrace (PTRACE_SINGLESTEP) function to control the child process to restart the stopped application and execute an instruction in the application, that is, control the parent process to call its child process, so that the child process restarts the stopped application, and executes the next instruction according to the running position of the application before it is stopped, and after executing the next instruction, control the child process to stop the application and record the running position of the application before it is stopped.
[0089] S304: modify the memory where the memory area to be monitored is located to an inaccessible state, and determine whether there is a next instruction of the currently executed instruction. If so, continue to execute S302 until all instructions in the application are executed. Embodiment 7:
[0090] In order to further and more accurately locate the instruction causing the heap memory to cross the boundary, based on the above embodiments, in the embodiment of the present application, the location of the code of the instruction in the application is recorded, including:
[0091] If it is identified that the area accessed by the instruction is the memory area to be monitored, the parent process receives the segmentation error signal sent by the child process, and calls the parent process to send a specific signal to the child process, so that the child process calls the signal processing function to perform a call stack backtrace, determine the location of the instruction code in the application program and record it.
[0092] In the embodiment of the present application, in order to record the location of the code of each instruction in the application when executing each instruction, therefore, for each instruction, if it is identified that the area accessed by the instruction is the memory area to be monitored, the parent process will receive the SIGSEGV signal sent by the child process, that is, when it is identified that the instruction is being executed, the electronic device calls the parent process, and the parent process calls the ptrace (PTRACE_CONT) function to send a specific signal to the child process, so that after receiving the specific signal, the child process calls the signal processing function in the positioning module, performs call stack backtracing, determines the location information of the code of the instruction in the application and records it. Among them, the specific signal can be the same signal pre-agreed by the parent process and the child process, and the specific signal is a specific signal registered by the module initialization function when the positioning module is initialized.
[0093] The signal processing function is one of the functions contained in the positioning module (such as Figure 2 As shown), and the signal processing function is used to control the child process to perform function call stack backtracing after receiving a specific signal.
[0094] Since the location information of the code of each instruction in the application is recorded in the embodiment of the present application, when it is determined that a heap memory out of bounds occurs when executing a certain instruction, the location of the code of the instruction in the application is determined based on the recorded location information of the code of each instruction in the application.
[0095] If it is determined that a heap memory boundary crossing occurs when a certain instruction is executed, the location of the code of the instruction in the application is determined based on the recorded location information of the code of each instruction in the application.
[0096] In an embodiment of the present application, the position of the code of each instruction in the application is recorded, so that when it is determined that a heap memory out-of-bounds occurs when executing a certain instruction, the code position of the instruction where the heap memory out-of-bounds occurs can be further and more accurately located based on the position of the code of each instruction recorded in the application. Embodiment 8:
[0097] On the basis of the above embodiments, in the embodiment of the present application, before modifying the memory where the memory area to be monitored is located to be accessible, the following steps are included:
[0098] Call the parent process to obtain the latest register values of the registers related to the child process;
[0099] It is determined whether the Program Counter (PC) pointer value in the latest register value is the same as the PC pointer value in the register value saved when the previous instruction is executed. If so, it is determined that the application program has been executed; if not, the step of modifying the memory where the memory area to be monitored is located to be accessible is continued.
[0100] In the embodiment of the present application, if it is recognized that the parent process receives the SIGSEGV signal sent by the child process, before modifying the memory where the memory area to be monitored is located to an accessible state, the electronic device calls the parent process and controls the parent process to call the ptrace (PTRACE_GETREGS) function to obtain the latest register value of the register related to the child process. The register related to the child process is the register used by the child process.
[0101] Get the register value of the register related to the child process saved when the previous instruction was executed, and determine whether the PC pointer value in the latest register value is the same as the PC pointer value in the register value saved when the previous instruction was executed. Based on the judgment result, determine whether the application has been executed.
[0102] If the PC pointer value in the latest register value is the same as the PC pointer value in the register value saved when the previous instruction was executed, it is determined that the application has been executed and that the child process can no longer continue to run at this time, and the parent process will enter a suspended state.
[0103] If the PC pointer value in the latest register value is different from the PC pointer value in the register value saved when executing the previous instruction, it is determined that the application has not been completed, and the step of modifying the memory where the memory area to be monitored is located to be accessible is continued, so that the child process continues to execute the next instruction.
[0104] The above embodiments are described below with a specific embodiment. Figure 4 One of the heap memory out-of-bounds location flow diagrams includes the following steps:
[0105] Step 1: The electronic device calls the fork function in the parent process to start the child process of the parent process. The parent process is used to monitor the running status of the child process, and the child process is used to execute the application.
[0106] Step 2: The child process adds the initialization code of the positioning module to the application, and uses the preset memory application function in the positioning module to replace the original memory application function contained in the application, and recompiles the application to obtain a compiled application. The child process starts the compiled application, and according to the memory area to be monitored, uses the preset memory application function in the positioning module to apply for the memory where the memory area to be monitored is located, and sets the memory where the memory area to be monitored is located to an inaccessible state.
[0107] Step 3: When the child process accesses the memory where the memory area to be monitored is located, a segmentation fault occurs and a SIGSEGV signal is sent to the parent process.
[0108] Step 4: After receiving the SIGSEGV signal sent by the child process, the parent process calls the ptrace (PTRACE_GETREGS) function to obtain and record the register values of all registers used by the child process, and calls the ptrace (PTRACE_PEEKDATA) function to obtain and record the data and storage location stored in the memory where the memory area to be monitored is located.
[0109] The parent process obtains the register values of all registers used by the child process that were last obtained and saved by itself, and determines whether the PC pointer value is consistent, that is, compares whether the PC pointer value in the register value obtained this time is the same as the PC pointer value in the register value obtained last time. If so, it is determined that the application has finished running and the child process cannot continue to run, and step eight is executed. If not, it is determined that the application can continue to run, and step five is executed.
[0110] The parent process obtains the data and storage location stored in the memory where the memory area to be monitored is located, and based on the data and storage location stored in the memory where the memory area to be monitored this time is located and the data and storage location stored in the memory where the memory area to be monitored last time was located, counts the change data and storage location of the data corresponding to the instruction executed this time compared to the data corresponding to the previous instruction.
[0111] Step 5: The parent process calls the ptrace (PTRACE_CONT) function to send a specific signal to the child process. The specific signal is the specific signal registered when the positioning module is initialized.
[0112] Step 6: After receiving the specific signal, the child process calls the signal processing function in the positioning module to perform call stack backtracing and determine the call stack backtracing information, that is, determine the location information of the instruction code in the application and record it.
[0113] Step 7: The parent process calls the first memory state modification function to modify the memory where the memory area to be monitored is located to an accessible state, and calls the ptrace (PTRACE_SINGLESTEP) function to control the child process to execute an instruction in the application. Then, the parent process calls the second memory state modification function to modify the memory where the memory area to be monitored is located to an inaccessible state, and calls the ptrace (PTRACE_CONT) function to make the child process continue to run, that is, execute the next instruction.
[0114] Step 8: The parent process enters the suspended state. At this point, the location information of the code of each instruction in the application program, the memory change data when executing each instruction, and the corresponding storage location are all recorded. For each instruction, it is determined whether the storage location corresponding to the change data of the data corresponding to the instruction compared with the data corresponding to the previous instruction is outside the memory area to be monitored. If so, it is determined that a heap memory cross-border occurs when executing the instruction, and the location of the code of each instruction recorded in the application program is determined in the application program.
[0115] The above embodiments are described below with another specific embodiment. Figure 5 The second diagram of the heap memory out-of-bounds location process includes the following steps:
[0116] Step 1: when a heap memory cross-border situation is identified when a certain application is executed, a memory area to be monitored where the heap memory cross-border situation occurs is located.
[0117] Step 2: Add the initialization code of the positioning module to the main function of the application.
[0118] Step 3: Use the code of the preset memory request function in the positioning module to replace the code of the original memory request function contained in the application program running when the boundary is crossed.
[0119] Step 4: Recompile the application and run the compiled application.
[0120] Step 5, when it is identified that the parent process is hung, the location of the code of the instruction that caused the heap memory to cross the boundary is determined in the application by analyzing the location information of the code of each instruction recorded in the application and the memory change data and corresponding storage location when each instruction is executed. Embodiment 9:
[0121] Based on the same technical concept, on the basis of the above embodiments, the present application provides a heap memory cross-boundary positioning device, Figure 6 A schematic diagram of a heap memory cross-boundary positioning device provided in an embodiment of the present application is shown in FIG. Figure 6 As shown, the device comprises:
[0122] The determination module 601 is used for locating the first heap memory area where the heap memory cross-border occurs and determining the first source code for applying for the first heap memory area when the heap memory cross-border occurs.
[0123] The application module 602 is used to determine the memory area to be monitored applied for the first source code based on the first heap memory area, wherein the memory area to be monitored includes the first heap memory area;
[0124] The positioning module 603 is used to execute each instruction in the application program. If it is identified that the executed instruction accesses the memory area to be monitored, for each instruction that accesses the memory area to be monitored, according to the storage location of the corresponding data recorded when the instruction is executed, it is determined whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored. If not, it is determined that a heap memory out of bounds occurs when the instruction is executed, and the location of the recorded code of the instruction in the application program is determined.
[0125] In a possible implementation, the application module 602 is specifically used to determine the target number of pages according to the number of first bytes stored in the first heap memory area, and determine the memory area to be monitored applied for the first source code using a preset memory application function according to the target number of pages.
[0126] In a possible implementation, the application module 602 is further configured to replace an original memory application function in the first source code with a preset memory application function before determining the memory area to be monitored applied for the first source code based on the first heap memory area to obtain a recompiled application;
[0127] The positioning module 603 is specifically used to execute each instruction in the recompiled application program.
[0128] In a possible implementation, the positioning module 603 is specifically configured to set the memory where the memory area to be monitored is located to an inaccessible state after determining the memory area to be monitored applied for the first source code and before executing each instruction in the recompiled application program.
[0129] In one possible implementation, the positioning module 603 is specifically used to loop through the following process in the order of instructions in the recompiled application until each instruction in the recompiled application is executed: for any instruction, if it is identified that the area accessed by the instruction is a memory area not to be monitored, the instruction is directly executed; if it is identified that the area accessed by the instruction is a memory area to be monitored, after configuring the memory where the memory area to be monitored is located to be accessible, the instruction is executed and the location of the code of the instruction in the application is recorded.
[0130] In a possible implementation, the positioning module 603 is specifically configured to modify the memory where the memory area to be monitored is located to an inaccessible state after executing the instruction if it is identified that the area accessed by the instruction is the memory area to be monitored.
[0131] In one possible implementation, the positioning module 603 is specifically used to, if it is identified that the area accessed by the instruction is a memory area to be monitored, the parent process receives a segmentation error signal sent by the child process, and calls the parent process to send a specific signal to the child process, so that the child process calls the signal processing function to perform a call stack backtrace, determine the location of the code of the instruction in the application and record it. Embodiment 10:
[0132] Based on the same technical concept, the present application also provides an electronic device, Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown in FIG. Figure 7 As shown, it includes: a processor 701, a communication interface 702, a memory 703 and a communication bus 704, wherein the processor 701, the communication interface 702, and the memory 703 communicate with each other through the communication bus 704;
[0133] The memory 703 stores a computer program. When the program is executed by the processor 701, the processor 701 performs the following steps:
[0134] When a heap memory cross-border occurs, locate the first heap memory area where the heap memory cross-border occurs, and determine the first source code for applying for the first heap memory area;
[0135] Based on the first heap memory area, determining a memory area to be monitored applied for by the first source code, wherein the memory area to be monitored includes the first heap memory area;
[0136] Execute each instruction in the application program. If it is recognized that the executed instruction accesses the memory area to be monitored, for each instruction that accesses the memory area to be monitored, determine whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored according to the storage location of the corresponding data recorded when the instruction is executed. If not, it is determined that a heap memory out of bounds occurs when executing the instruction, and the location of the recorded code of the instruction in the application program is determined.
[0137] In a possible implementation, the processor 701 is specifically configured to determine a target number of pages based on the number of first bytes stored in the first heap memory area, and determine a memory area to be monitored applied for the first source code using a preset memory application function based on the target number of pages.
[0138] In one possible implementation, the processor 701 is further used to, based on the first heap memory area, before determining the memory area to be monitored as applied for by the first source code, replace the original memory application function in the first source code with a preset memory application function to obtain a recompiled application; and execute each instruction in the recompiled application.
[0139] In a possible implementation, the processor 701 is specifically configured to set the memory where the memory area to be monitored is located to an inaccessible state after determining the memory area to be monitored applied for the first source code and before executing each instruction in the recompiled application program.
[0140] In one possible implementation, the processor 701 is specifically configured to loop through the following process in the order of instructions in the recompiled application until each instruction in the recompiled application is executed: for any instruction, if it is identified that the area accessed by the instruction is a memory area not to be monitored, the instruction is directly executed; if it is identified that the area accessed by the instruction is a memory area to be monitored, after configuring the memory where the memory area to be monitored is located as accessible, the instruction is executed, and the location of the code of the instruction in the application is recorded.
[0141] In a possible implementation, the processor 701 is specifically configured to modify the memory where the memory area to be monitored is located to an inaccessible state after executing the instruction if it is identified that the area accessed by the instruction is the memory area to be monitored.
[0142] In one possible implementation, the processor 701 is specifically used to, if it is recognized that the area accessed by the instruction is a memory area to be monitored, the parent process receives a segmentation fault signal sent by the child process, and calls the parent process to send a specific signal to the child process, so that the child process calls the signal processing function to perform a call stack backtrace, determine the location of the code of the instruction in the application program and record it.
[0143] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0144] The communication interface 702 is used for communication between the electronic device and other devices.
[0145] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0146] The above-mentioned processor can be a general-purpose processor, including a central processing unit, a network processor (NP), etc.; it can also be a digital signal processing processor (DSP), an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, etc. Embodiment 11:
[0147] Based on the same technical concept, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program that can be executed by an electronic device. When the program runs on the electronic device, the electronic device implements any of the above embodiments when executing.
[0148] The above-mentioned computer-readable storage medium can be any available medium or data storage device that can be accessed by the processor in the electronic device, including but not limited to magnetic storage such as floppy disks, hard disks, magnetic tapes, magneto-optical disks (MO), etc., optical storage such as CDs, DVDs, BDs, HVDs, etc., and semiconductor storage such as ROM, EPROM, EEPROM, non-volatile memory (NANDFLASH), solid-state drives (SSD), etc.
[0149] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0150] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0151] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0152] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0153] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A method for locating heap memory out-of-bounds, characterized in that: The method comprises: When a heap memory cross-border occurs, locating a first heap memory area where the heap memory cross-border occurs, and determining a first source code for applying for the first heap memory area; A preset memory application function is used to replace the original memory application function in the first source code to obtain a recompiled application program; based on the first heap memory area, the preset memory application function is used to apply for a memory area to be monitored for the first source code, wherein the memory area to be monitored includes the first heap memory area, and the preset memory application function is used to record information of the memory area to be monitored after the preset memory application function is used to apply for the memory area to be monitored; Execute each instruction in the recompiled application program. If it is recognized that the executed instruction accesses the memory area to be monitored, for each instruction that accesses the memory area to be monitored, determine whether the data corresponding to the instruction is written into the first heap memory area contained in the memory area to be monitored according to the storage location of the corresponding data recorded when the instruction is executed. If not, determine that a heap memory out of bounds occurs when executing the instruction, and determine the location of the recorded code of the instruction in the application program.
2. The method according to claim 1, characterized in that Based on the first heap memory area, a preset memory application function is used to apply for a memory area to be monitored for the first source code, including: The target number of pages is determined according to the number of first bytes stored in the first heap memory area, and the memory area to be monitored applied for the first source code is determined according to the target number of pages using a preset memory application function.
3. The method according to claim 1, characterized in that: After the preset memory application function is used to apply for the memory area to be monitored for the first source code, and before each instruction in the recompiled application is executed, the method includes: The memory where the memory area to be monitored is located is set to an inaccessible state.
4. The method according to claim 3, characterized in that The executing each instruction in the recompiled application program includes: The following process is executed in a loop according to the order of the instructions in the recompiled application program until each instruction in the recompiled application program is executed: For any instruction, if it is identified that the area accessed by the instruction is not a memory area to be monitored, the instruction is executed directly; if it is identified that the area accessed by the instruction is the memory area to be monitored, the instruction is executed after the memory where the memory area to be monitored is located is configured to be accessible, and the location of the code of the instruction in the application is recorded.
5. The method according to claim 4, characterized in that If it is identified that the area accessed by the instruction is the memory area to be monitored, after executing the instruction, the method includes: The memory where the memory area to be monitored is located is modified to be inaccessible.
6. The method according to claim 4, characterized in that The location of the code recording the instruction in the application program includes: If it is identified that the area accessed by the instruction is the memory area to be monitored, the parent process receives the segmentation error signal sent by the child process, and calls the parent process to send a specific signal to the child process, so that the child process calls the signal processing function to perform a call stack backtrace, determines the location of the code of the instruction in the application, and records it.
7. A heap memory out-of-bounds location device, characterized in that: The device comprises: A determination module, used for locating a first heap memory area where the heap memory cross-border occurs, and determining a first source code for applying for the first heap memory area when the heap memory cross-border occurs. An application module, configured to replace the original memory application function in the first source code with a preset memory application function to obtain a recompiled application program; based on the first heap memory area, using the preset memory application function to apply for a memory area to be monitored for the first source code, wherein the memory area to be monitored includes the first heap memory area, and the preset memory application function is configured to record information of the memory area to be monitored after applying for the memory area to be monitored with the preset memory application function; A positioning module is used to execute each instruction in the recompiled application program. If it is identified that the executed instruction accesses the memory area to be monitored, for each instruction accessing the memory area to be monitored, according to the storage location of the corresponding data recorded when the instruction is executed, it is determined whether the data corresponding to the instruction is written into the first heap memory area included in the memory area to be monitored. If not, it is determined that a heap memory cross-border occurs when the instruction is executed, and the location of the recorded code of the instruction in the application program is determined.
8. An electronic device, characterized in that: The electronic device includes at least a processor and a memory, and the processor is used to implement the steps of the heap memory cross-boundary location method as described in any one of claims 1 to 6 when executing a computer program stored in the memory.
9. A computer storage medium, characterized in that It stores a computer program executable by an electronic device. When the program runs on the electronic device, the electronic device executes the steps of the heap memory cross-boundary locating method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Method for detecting memory accessing outside
CN101110042A
Method and device for managing heap corruption
CN106682492A
Memory border crossing detection method, device and equipment and storage medium
CN111124921A
Method and device for detecting boundary crossing of system memory
CN114625646A
Memory cross-border detection method and device, electronic equipment and storage medium
CN114691469A
