Memory cross-border positioning method and device and storage medium

By identifying and setting protected memory segments in an embedded system and using the memory management unit to capture memory out-of-bounds locations, the problem of inefficient memory out-of-bounds location in an embedded system is solved, and efficient and accurate memory out-of-bounds locations are achieved.

CN119718735APending Publication Date: 2025-03-28TOPSEE TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411671019.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-21
Publication Date
2025-03-28

AI Technical Summary

Technical Problem

In embedded systems, it is difficult for the prior art to accurately locate and solve the memory out-of-bounds problem in resource-constrained environments, and traditional tools have problems such as inefficiency and high resource consumption in detection and positioning.

Method used

By identifying possible contamination of protected memory segments and allocating continuous memory blocks before and after them, setting them to read-only or unexecutable mode, the memory management unit is used to respond to CPU access requests to capture where memory outage occurs.

Benefits of technology

It realizes efficient and accurate memory out-of-bound positioning in embedded systems, reduces resource consumption and improves the efficiency of memory out-of-bound positioning.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119718735A_ABST
    Figure CN119718735A_ABST
Patent Text Reader

Abstract

The invention discloses a memory cross-border positioning method and device and a storage medium, and the method comprises the steps: recognizing a protection memory section which may have a pollution possibility, and distributing continuous memory blocks in front of and behind the protection memory section to obtain a memory protection region, setting the attribute of the memory protection area as a system mode or a non-executable mode; then calling the memory management unit to respond to a CPU access request; and when the response of the memory management unit is abnormal, capturing and positioning a position where memory crossing occurs. By utilizing the characteristic that when a memory is set to be in a read-only mode or a non-executable mode, writing cannot be carried out, when positioning cross-border occurs, a pollution source tries to access a memory protection area, and at the moment, a read-only attribute or a non-executable attribute set in the memory protection area causes insufficient access authority, so that response abnormity occurs; the position where memory crossing occurs can be accurately judged by tracking the error position, memory crossing positioning is achieved, the efficiency of memory crossing positioning is improved, and resource consumption is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a memory out-of-bounds location method, device and storage medium. Background Art

[0002] Memory safety is a critical issue in computer systems, especially in embedded systems. Memory out-of-bounds refers to a program trying to write data into a buffer that exceeds its capacity, causing the data to overflow into adjacent memory areas. It is a common memory safety vulnerability that can lead to serious consequences such as data corruption, information leakage, or remote code execution. This problem is more prominent in resource-constrained embedded systems because they usually lack sophisticated memory protection mechanisms.

[0003] In current related technologies, developers mainly rely on static code analysis, code review and testing to prevent memory out-of-bounds problems, using debugging tools such as Valgrind or AddressSanitizer for analysis and judgment. Although these tools perform well in desktop and server environments, they require large memory overhead and computing resources, and are limited in application in embedded systems. They can usually only detect the occurrence of memory out-of-bounds, but it is difficult to accurately locate the source location that causes the out-of-bounds. Summary of the invention

[0004] The following is a summary of the subject matter described in detail herein. This summary is not intended to limit the scope of the claims.

[0005] The embodiments of the present application provide a memory cross-boundary location method, device and storage medium, which can provide accurate memory cross-boundary location and improve the efficiency of memory cross-boundary location.

[0006] In a first aspect, an embodiment of the present application provides a memory out-of-bounds location method, comprising the following steps: Identify protected memory segments that have the potential for contamination; Allocating continuous memory blocks before and after the protected memory segment to obtain a memory protection zone, and calling a memory management unit to set the attribute of the memory protection zone to a read-only mode or a non-executable mode; Calling the memory management unit to respond to the CPU access request; When the memory management unit responds to the exception, it captures and locates the location where the memory out-of-bounds occurs.

[0007] According to the memory cross-bounds location method provided by some embodiments of the present application, the step of identifying a protected memory segment with a possibility of being polluted includes: Calling software analysis tools to monitor memory access patterns to determine memory access records before and after a memory cross-border event occurs; A memory segment with a high frequency of memory cross-boundary access is determined according to the memory access record, and the memory segment with a high frequency of memory cross-boundary access is determined as a protected memory segment.

[0008] According to the memory cross-bounds location method provided by some embodiments of the present application, the step of identifying a protected memory segment with a possibility of being polluted includes: Acquire system business logic and data structure, and determine key memory areas, where the key memory areas are memory segments containing important data structures or frequently accessed memory segments; The critical memory area is determined as a protected memory segment.

[0009] According to the memory out-of-bounds locating method provided by some embodiments of the present application, the CPU access request generates a virtual address generated by the CPU; The calling of the memory management unit to respond to the CPU access request includes: Acquire the virtual address, and send the virtual address to the memory management unit; The virtual address is converted into a physical address by the memory management unit.

[0010] According to the memory out-of-bounds location method provided by some embodiments of the present application, the memory management unit responds to the exception including: The physical address access rights obtained by the memory management unit through conversion are insufficient; Alternatively, the memory management unit fails to convert the virtual address into a physical address.

[0011] According to the memory cross-border locating method provided by some embodiments of the present application, capturing and locating the location where the memory cross-border occurs includes: Confirming that the memory management unit responds to an abnormal location in the protected memory segment, and obtaining stack information of the protected memory segment; According to the stack information, traverse the call stack using a stack backtrace library; Analyze the function addresses of each layer of the call stack and map the function addresses to source code files and line numbers; Print the source code file and the line number.

[0012] According to the memory out-of-bounds location method provided by some embodiments of the present application, when the memory management unit responds to the exception and captures and locates the location where the memory out-of-bounds occurs, the method further includes: Generate a location report, and send the location report to a log; The attributes of the protected memory segment are reset, and the memory management unit is called to respond to the CPU access request.

[0013] In a second aspect, an embodiment of the present application provides a memory cross-border positioning device, including: A memory segment identification module, used to identify protected memory segments that may be contaminated; A protection allocation module, used for allocating continuous memory blocks before and after the protection memory segment to obtain a memory protection zone, and calling a memory management unit to set the attribute of the memory protection zone to a read-only mode or a non-executable mode; A request response module, used for calling the memory management unit to respond to the CPU access request; The location capture module is used to capture and locate the location where the memory crosses the boundary when the memory management unit responds to an exception.

[0014] In a third aspect, an embodiment of the present application provides an electronic device, including: at least one processor; at least one memory for storing at least one program; When at least one of the programs is executed by at least one of the processors, the memory positioning out-of-bounds method as described in the first aspect of the present application is implemented.

[0015] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein a computer program executable by a processor is stored, and when the computer program executable by the processor is executed by the processor, it is used to implement the method for memory location out-of-bounds as described in the first aspect. In another aspect, an embodiment of the present application also provides an electronic device, including: at least one processor; at least one memory for storing at least one program; When at least one of the programs is executed by at least one of the processors, the memory out-of-bounds location method described above is implemented.

[0016] The embodiments of the present application include at least the following beneficial effects: In the embodiment of the present application, by identifying the protected memory segment that may be contaminated, by allocating continuous memory blocks before and after the protected memory segment, a memory protection zone is obtained, and the attribute of the memory protection zone is set to system mode or non-executable mode; then the memory management unit is called to respond to the CPU access request; when the memory management unit responds abnormally, the location where the memory out-of-bounds occurs is captured and located. By utilizing the characteristic that the memory cannot be written when it is set to read-only mode or non-executable mode, when a positioning out-of-bounds occurs, the pollution source will try to access the memory protection zone, and at this time, the read-only attribute or non-executable attribute set in the memory protection zone will cause insufficient access rights and thus a response abnormality. Tracking the error location can accurately determine the location where the memory out-of-bounds occurs, realize memory out-of-bounds location, improve the efficiency of memory out-of-bounds location, and reduce resource consumption.

[0017] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or understood by practicing the present application. The purpose and other advantages of the present application can be realized and obtained through the structures particularly pointed out in the description and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] The accompanying drawings are used to provide further understanding of the technical solution of the present application and constitute a part of the specification. Together with the embodiments of the present application, they are used to explain the technical solution of the present application and do not constitute a limitation on the technical solution of the present application.

[0019] Figure 1 A schematic diagram of the steps of a memory cross-bounds location method provided in an embodiment of the present application; Figure 2 A schematic diagram of steps for identifying a protected memory segment provided in an embodiment of the present application; Figure 3 A schematic diagram of another step of protecting memory segment identification provided in an embodiment of the present application; Figure 4 A schematic diagram of the steps for capturing the location of a memory out-of-bounds provided in an embodiment of the present application; Figure 5 A schematic diagram of a specific process of a memory cross-boundary location method provided in an embodiment of the present application; Figure 6 A schematic diagram of the structure of a memory cross-border locating device provided in an embodiment of the present application; Figure 7 It is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0020] The present application is further described below in conjunction with the accompanying drawings and specific embodiments. The described embodiments should not be regarded as limiting the present application, and all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present application.

[0021] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.

[0022] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.

[0023] Before further describing the embodiments of the present application in detail, the nouns and terms involved in the embodiments of the present application are explained. The nouns and terms involved in the embodiments of the present application are subject to the following interpretations.

[0024] An embedded system is a computer system designed for a specific task. Unlike general-purpose computing devices (such as personal computers), embedded systems are usually integrated into larger systems or products to perform specific functions. These systems can be standalone or part of a larger system, such as cars, mobile phones, home appliances, medical devices, etc. Embedded systems are usually designed to perform specific tasks, so their hardware and software are optimized to meet specific application requirements. Due to cost and physical size constraints, the processing power, memory, and power consumption of embedded systems are often strictly limited.

[0025] Buffer Overflow or Buffer Overrun refers to a program trying to write data beyond the buffer capacity, causing the data to overflow into the adjacent memory area. This is a common memory security vulnerability. This error may not only cause the program to crash, but may also be maliciously exploited to execute arbitrary code, thus causing security vulnerabilities. The main reasons for memory overflow include: 1. Fixed-size buffer: The programmer allocates a fixed-size memory space for the buffer, but ignores boundary checking in actual operation and writes data to the buffer that exceeds its capacity; 2. Improper string processing: The null character (\0) is not correctly terminated when processing the string, causing subsequent operations to exceed the expected range; 3. Array out of bounds: When accessing an array, the index value exceeds the valid range of the array.

[0026] Memory out-of-bounds may cause the program to crash due to accessing illegal memory addresses; data written out of bounds may overwrite other variables or data structures, causing data corruption; attackers can use memory out-of-bounds vulnerabilities to inject malicious code, perform unauthorized operations, or even completely control the target system. Therefore, it is very important to locate the location of memory out-of-bounds and handle it accordingly.

[0027] Memory safety is a critical issue in computer systems, especially in embedded systems. This problem is more prominent in resource-constrained embedded systems because they usually lack sophisticated memory protection mechanisms.

[0028] In current related technologies, developers mainly rely on static code analysis, code review and testing to prevent memory out-of-bounds problems, using debugging tools such as Valgrind or AddressSanitizer for analysis and judgment. Although these tools perform well in desktop and server environments, they require large memory overhead and computing resources, and are limited in application in embedded systems. They can usually only detect the occurrence of memory out-of-bounds, but it is difficult to accurately locate the source location that causes the out-of-bounds.

[0029] Based on this, the embodiments of the present application propose a memory cross-border location method, device and storage medium, aiming to provide an accurate memory cross-border location method and improve the efficiency of memory cross-border location. By identifying the protected memory segment that may be contaminated, by allocating continuous memory blocks before and after the protected memory segment, a memory protection zone is obtained, and the attribute of the memory protection zone is set to system mode or non-executable mode; then the memory management unit is called to respond to the CPU access request; when the memory management unit responds abnormally, the location where the memory cross-border occurs is captured and located. Utilizing the characteristic that the memory cannot be written when it is set to read-only mode or non-executable mode, when a positioning cross-border occurs, the pollution source will try to write to the memory protection zone, resulting in a response exception. By tracking the error position, the location where the memory cross-border occurs can be accurately determined, and memory cross-border location can be achieved, the efficiency of memory cross-border location can be improved, and resource consumption can be reduced.

[0030] First, please refer to Figure 1 , is a schematic diagram of the steps of a memory cross-boundary location method provided in an embodiment of the present application, such as Figure 1 As shown, in the embodiment of the present application, the method for locating memory out-of-bounds may include but is not limited to steps S110 to S140: Step S110: identifying protected memory segments that may be contaminated.

[0031] It should be noted that in the embodiment of the present application, considering that memory out-of-bounds may cause multiple memory segments to be contaminated, these segments include but are not limited to the stack, heap, global data section and code segment. Therefore, it is necessary to first identify the memory segments that may be contaminated, and use step S110 to identify the protected memory segments that may be contaminated.

[0032] Next, a method for identifying protected memory segments in an embodiment of the present application is described in detail.

[0033] For example, please refer to Figure 2 , is a schematic diagram of the steps of protecting memory segment identification provided by an embodiment of the present application, such as Figure 2As shown, in a feasible embodiment of the present application, the identification of protected memory segments can be achieved by monitoring the memory access mode. When the protected memory segments are identified by using the method of monitoring the memory access mode, it may include but is not limited to steps S111a to S112a: Step S111a: calling a software analysis tool to monitor the memory access pattern to determine the memory access records before and after the memory cross-border occurs.

[0034] It should be noted that in the embodiments of the present application, a lightweight performance analysis (profiling) tool can be called to monitor the memory access mode. The lightweight profiling tool can better perform performance analysis in a resource-constrained environment such as an embedded system.

[0035] Specifically, in a feasible embodiment of the present application, it is necessary to analyze the protected memory segments in the Linux system, and the Valgrind performance analysis tool can be used to determine the memory access records before and after the memory cross-border occurs by analyzing the log file. In addition to Valgrind, the embodiment of the present application can also use LTTng (Linux Trace Toolkit Next Generation) or ftrace to determine the memory access records before and after the memory cross-border occurs in the Linux system.

[0036] In another feasible embodiment of the present application, it is necessary to analyze the protected memory segments in C and C++ programs. The gprof performance analysis tool can be used. Gprof can generate a detailed performance report, listing the number of calls to each function, the total execution time, the average time per call and other information, and help identify which functions take up the most CPU time. It can better help determine the memory access records before and after the memory cross-border occurs.

[0037] It is understandable that, according to the embedded system actually used, a corresponding lightweight profiling tool can be selected to determine the memory access records before and after the memory cross-border occurs. After the access records are obtained, step S112a can be executed according to the obtained access records.

[0038] Step S112a: Determine a memory segment with a high frequency of memory cross-boundary access according to the memory access record, and determine the memory segment with a high frequency of memory cross-boundary access as a protected memory segment.

[0039] Exemplarily, in an embodiment of the present application, after obtaining the memory access record, detailed information of the memory access error is extracted therefrom, especially the accessed memory address and context, and then the access frequency of each memory address is counted, and the protection memory segment is determined based on the access frequency of each memory.

[0040] Specifically, in a feasible embodiment of the present application, the memory addresses with the highest access frequency are determined. These addresses may be hot spots for memory out-of-bounds access. For example: the high-frequency access address is 0x60200000ef88, and the number of accesses is 100 times; the second high-frequency access address is 0x60200000ef90, and the number of accesses is 50 times, and so on, then such high-frequency access addresses are determined to be protected memory segments.

[0041] Through step S111a and step S112a, the embodiment of the present application determines the memory access record by using a software analysis tool to identify the protected memory segment with the possibility of pollution, so as to facilitate the subsequent protection operation setting.

[0042] Please refer to Figure 3 , is a schematic diagram of another step of protecting memory segment identification provided by an embodiment of the present application, such as Figure 3 As shown, in a feasible embodiment of the present application, the identification of protected memory segments can be achieved by analyzing business logic and data structure. When the method of monitoring memory access patterns is used to identify protected memory segments, it may include but is not limited to steps S111b to S112b: Step S111b: Acquire the service logic and data structure of the system, and determine the key memory area, where the key memory area is a memory segment containing an important data structure or a frequently accessed memory segment.

[0043] It is understood that in the embodiments of the present application, business logic refers to a series of rules, algorithms and processes used in the software system to achieve specific business goals. It directly reflects the functional requirements of the system and is a bridge between user needs and technical implementation; data structure refers to the organization and storage method of data in a computer. By analyzing business logic and data structure, memory segments containing important data structures or frequently accessed memory segments, i.e., critical memory areas, can be determined.

[0044] Step S112b: determine the critical memory area as a protected memory segment.

[0045] The determined critical memory area is the protected memory segment that needs to be protected, so the critical memory area is determined as the protected memory segment.

[0046] Specifically, in one embodiment of the present application, by analyzing the system business logic and data structure, the key memory areas that may be affected are determined, especially when memory out-of-bounds occurs, the business logic of the system is understood in detail, including the functions of each business module, the data processing flow and the dependencies between each other, and the key data structures used in the system are understood, including the organization, storage and access methods of the data. For example, suppose the system has three business modules A, B, and C, each of which has its specific functions and data processing logic; for example, business B may use a complex linked list to manage data, while business A may use a hash table to quickly find data.

[0047] According to the business logic and data structure, determine which data structures are critical, that is, data structures that have a significant impact on system performance, stability, and security. For example, business B has a core linked list for storing and managing important data. Through performance monitoring and dynamic analysis, find out the frequently accessed memory segments. These segments are usually performance bottlenecks or potential memory problem points. For example, there is a frequently accessed array in business B for real-time data processing. Business B may have an operation of looping through a linked list. If the length of the linked list is calculated incorrectly, it may cause memory out of bounds. Memory out of bounds can cause the system to read or write memory areas that should not be accessed, causing various unpredictable behaviors such as data corruption, program crashes, security vulnerabilities, etc. For example, if the linked list traversal operation of business B goes out of bounds, it may overwrite the data of business A or business C, causing instability in the entire system. When it is found that the operating logic of business B does not meet expectations, logical troubleshooting can determine the critical memory area.

[0048] Through step S111b and step S112b, the protected memory segment with the possibility of pollution can be identified to facilitate the subsequent protection operation setting.

[0049] Step S120: Allocate continuous memory blocks before and after the protected memory segment to obtain a memory protection zone, and call a memory management unit to set the attribute of the memory protection zone to a read-only mode or a non-executable mode.

[0050] Exemplarily, in an embodiment of the present application, once a protected memory segment with the possibility of pollution is identified, continuous memory blocks will be dynamically allocated before and after it to obtain a memory protection area; a memory allocation function (such as malloc or corresponding memory allocation function in other languages) can be used to allocate a memory block before and after the protected memory segment.

[0051] It should be noted that the size of the protection zone can be adjusted according to system resources and security requirements, and is usually the size of one or more memory pages. Record the starting address and size of the protection zone for subsequent settings and management.

[0052] Specifically, in an embodiment of the present application, the malloc function in the C language is used to allocate an additional memory segment before and after the identified memory segment, thereby obtaining a memory protection zone; the size of the protection zone is set to the smallest addressable memory unit in the operating system, such as a memory page size of 4KB.

[0053] The memory protection zone is obtained by allocating continuous memory blocks before and after the protected memory segment, and the memory management module is further called to set the attribute of the memory protection zone to read-only mode or non-executable mode.

[0054] Exemplarily, in one embodiment of the present application, a memory management unit (MMU) is used to set the page attribute of the protected area to read-only; the access rights of the protected area are modified through the memory protection API (such as mprotect) provided by the operating system; the page table entry corresponding to the protected area is marked as read-only, and write operations are prohibited.

[0055] In another embodiment of the present application, under the condition of system support, the attribute of the memory protection zone can be further set to be non-executable to provide additional security protection.

[0056] By setting the attribute of the memory protection zone to read-only or non-executable, the data in the memory protection zone is protected. When a memory cross-border occurs, the read-only and non-executable attribute characteristics are used to locate the memory cross-border.

[0057] Step S130: calling the memory management unit to respond to the CPU access request.

[0058] It should be noted that in the embodiment of the present application, the access request of the CPU carries a virtual address generated by the CPU, and calling the memory management unit to respond to the CPU access request includes: Get the virtual address and send the virtual address to the memory management unit; The virtual address is converted to a physical address by the memory management unit.

[0059] It should be noted that the Memory Management Unit (MMU) is an important component in the computer system. The MMU can convert the virtual address used by the application into an actual physical address. When responding to a CPU access request, the MMU receives the virtual address sent by the CPU and tries to convert it into a physical address.

[0060] Exemplarily, in an embodiment of the present application, responding to a CPU access request by calling an MMU includes the following steps: The MMU receives the virtual address in the access request from the CPU; Splitting the received virtual address into a virtual page number and an offset within the page; Use the virtual page number to find the corresponding page table entry in the page table; If the page table entry exists and is valid, the MMU combines the physical page frame number in the page table entry with the offset within the page into a physical address.

[0061] Through the above process, the MMU can complete the CPU's access request response. Since the page table entry pointed to by the virtual page number in the virtual address generated by the CPU does not exist or is invalid, a response exception will be triggered. At the same time, when responding to the access request, the MMU will also check the access rights of the page table entry. If the memory in the protected area is set to non-writable, the write permission bit of the page table entry is 0, and the MMU will also trigger a response exception.

[0062] By responding to the CPU's access request through the MMU, it is possible to determine whether a memory out-of-bounds error occurs. That is, when a response exception occurs, it means that a memory out-of-bounds error occurs, and step S140 is executed at this time.

[0063] Step S140: When the memory management unit responds to an exception, the location where the memory boundary violation occurs is captured and located.

[0064] It can be understood that in the embodiment of the present application, when the memory management unit responds to an exception, it is determined as a memory out-of-bounds situation based on the type of the response exception, and the location of the memory out-of-bounds situation is further captured.

[0065] As described above, in an embodiment of the present application, the memory protection zone is set to a read-only mode or a non-executable mode. Therefore, if a memory out-of-bounds occurs when responding to a CPU access request, the address requested by the CPU to access will point to a memory protection zone with a read-only or non-executable attribute, resulting in a response exception. At this time, it is only necessary to locate the location of the response exception.

[0066] It should be noted that, in the embodiment of the present application, the memory management unit responds to an abnormality including: The physical address access rights obtained by the memory management unit are insufficient; Or, the memory management unit fails to convert a virtual address to a physical address.

[0067] Exemplarily, in one embodiment of the present application, the attribute of the memory protection zone is set to read-only mode. When responding to a CPU access request, a memory out-of-bounds occurs, and the memory management unit does not have the authority to convert the virtual address carried in the CPU response request into a physical address. At this time, a response error message will appear.

[0068] In another embodiment of the present application, the attribute of the memory protection zone is set to a non-executable mode. When responding to a CPU access request, a memory out-of-bounds occurs, and the memory management unit fails to convert the virtual address carried in the CPU response request into a physical address, and a response error message appears.

[0069] In an embodiment of the present application, when an error occurs, the location of the error can be directly located. Because the attributes of the memory protection zone in the CPU access request are set to read-only and non-executable, when a memory out-of-bounds error occurs, the error directly points to the location where the error occurs. Therefore, the specific location that causes the response error can be directly printed out, thereby achieving accurate positioning of the memory out-of-bounds error.

[0070] Please refer to Figure 4 , is a schematic diagram of a step of capturing a memory out-of-bounds location provided by an embodiment of the present application, such as Figure 4 As shown, in the embodiment of the present application, when the system has a location out-of-bounds situation, the memory out-of-bounds location capture may include but is not limited to steps S141 to S144: Step S141: confirm that the memory management unit responds that the abnormal location is in the protected memory segment, and obtain stack information of the protected memory segment.

[0071] Exemplarily, in one embodiment of the present application, the stack pointer of the protected memory segment is read, the stack pointer points to the top of the current call stack, and starting from the stack pointer, the stack frame is read layer by layer to obtain the return address and parameters of each function call.

[0072] By acquiring the stack information, step S142 is performed to perform a backtrace library traversal of the call stack.

[0073] Step S142: traverse the call stack using a stack backtrace library according to the stack information.

[0074] Exemplarily, in an embodiment of the present application, the backtrace and backtrace_symbols functions in the GNU C Library (glibc) or a more advanced library such as libunwind can be used as a stack backtrace library for traversal. In a feasible embodiment of the present application, the backtrace function is used to obtain the address list of the call stack through the backtrace function, and the backtrace_symbols function is used to convert the address list into symbol information (function name, file name and line number).

[0075] Through step S142, the call stack can be traversed to obtain the function address that responds to the error, thereby executing step S143.

[0076] Step S143: Analyze the function address of each layer of the call stack, and map the function address to the source code file and line number.

[0077] Exemplarily, in an embodiment of the present application, after obtaining the function address of the response error, the function address of each layer of the call stack can be analyzed and the address can be mapped to the source code file and line number using the addr2line tool. In the present application, the function address mapping can be implemented using corresponding tools, and the present application does not impose too many restrictions on the selection of mapping tools.

[0078] Step S144: print the source code file and line number.

[0079] It can be understood that the source code file and line number after mapping are the locations where the memory cross-bounds occurs. After the source code file and line number are obtained, they can be printed.

[0080] In the embodiment of the present application, through steps S141 to S144, the location of the memory out-of-bounds can be captured and the location where the memory out-of-bounds occurs can be accurately located.

[0081] It should be noted that, in the embodiment of the present application, when the memory management unit responds to an exception and captures and locates the location where the memory out-of-bounds occurs, the memory out-of-bounds location method further includes: Generate a location positioning report and send the location positioning report to the log; Reset the attributes of the protected memory segment and call the memory management unit to respond to CPU access requests.

[0082] It is understood that in the embodiment of the present application, the positioning results are recorded and reported to the developer: a detailed report containing information such as the exception type, memory address, source code location, etc. can be generated. It can be output to the log immediately or sent through the remote diagnostic interface. This allows the developer to decide whether to interrupt program execution or continue running according to the system settings.

[0083] Depending on system requirements, you can choose to resume program execution after reporting a problem: reset the memory attributes of the protected area to allow normal access; and resume the interrupted program execution process. Consider implementing an automated memory repair mechanism, such as consistency checking and repairing of data structures.

[0084] In an embodiment of the present application, a protected memory segment that may be contaminated is identified, continuous memory blocks are allocated before and after the protected memory segment to obtain a memory protection zone, and the attribute of the memory protection zone is set to a system mode or a non-executable mode; the memory management unit is then called to respond to the CPU access request; when the memory management unit responds with an exception, the location where the memory out-of-bounds occurs is captured and located.

[0085] The embodiments of the present application include at least the following beneficial effects: utilizing the characteristic that the memory cannot be written when it is set to read-only mode or non-executable mode, when a positioning out-of-bounds occurs, the pollution source will attempt to access the memory protection zone, and at this time, the read-only attribute or non-executable attribute set for the memory protection zone will lead to insufficient access rights and thus a response exception. Tracking the error location can accurately determine the location where the memory out-of-bounds occurs, achieve memory out-of-bounds positioning, improve the efficiency of memory out-of-bounds positioning, and reduce resource consumption.

[0086] For example, please refer to Figure 5 , is a specific flow chart of a memory cross-boundary location method provided in an embodiment of the present application, such as Figure 5 As shown, in the embodiment of the present application, when performing memory out-of-bounds positioning, the following steps may be specifically included but not limited to: Identify protected memory segments that have the potential for contamination; Allocate continuous memory blocks before and after the protected memory segment to obtain the memory protection zone; Call the memory management unit to set the attribute of the memory protection area to read-only mode or to non-executable mode; Call the memory management unit to respond to the CPU access request and determine whether a response exception occurs; If no response exception occurs, continue to respond to the CPU access request; If a response exception occurs, confirm that the memory management unit response exception location is in the protected memory segment, and obtain stack information of the protected memory segment; Based on the stack information, use the stack backtrace library to traverse the call stack; Analyze the function addresses of each layer of the call stack and map the function addresses to source code files and line numbers; Print source code file and line number; Generate a location positioning report and send the location positioning report to the log; Reset the attributes of the protected memory segment, call the memory management unit to respond to the CPU access request, or terminate the program.

[0087] Through the embodiments of the present application, the inability to write when the memory is set to read-only mode or non-executable mode is utilized to accurately determine the location where the memory cross-bounds occurs, implement memory cross-bounds location, improve the efficiency of memory cross-bounds location, and reduce resource consumption.

[0088] Please refer to Figure 6 , is a schematic diagram of the structure of a memory cross-border positioning device provided in an embodiment of the present application, such as Figure 6 As shown, in the embodiment of the present application, the memory cross-boundary location device 100 for performing memory cross-boundary location may include but is not limited to: A memory segment identification module 110, for identifying protected memory segments that may be contaminated; A protection allocation module 120, configured to allocate continuous memory blocks before and after the protection memory segment to obtain a memory protection zone, and call a memory management unit to set the attribute of the memory protection zone to a read-only mode or a non-executable mode; A request response module 130, configured to call the memory management unit to respond to a CPU access request; The location capture module 140 is used to capture and locate the location where the memory boundary crossing occurs when the memory management unit responds to an exception.

[0089] It should be noted that since the memory out-of-bounds positioning device 100 of this embodiment can implement the memory out-of-bounds positioning method of the previous embodiment, the memory out-of-bounds positioning device 100 of this embodiment and the memory out-of-bounds positioning method of the previous embodiment have the same technical principles and the same beneficial effects. In order to avoid repetition, they will not be repeated here.

[0090] Please refer to Figure 7 The embodiment of the present application further discloses an electronic device, the electronic device 1100 comprising: at least one processor 1101; At least one memory 1102, used to store at least one program; When at least one program is executed by at least one processor 1101, the above memory out-of-bounds location method is implemented.

[0091] The embodiment of the present application also discloses a computer-readable storage medium, which stores a computer program executable by a processor. When the computer program executable by the processor is executed by the processor, it is used to implement the memory out-of-bounds location method as described above.

[0092] An embodiment of the present application also discloses a computer program product, including a computer program or computer instructions, wherein the computer program or computer instructions are stored in a computer-readable storage medium, a processor of an electronic device reads the computer program or computer instructions from the computer-readable storage medium, and the processor executes the computer program or computer instructions, so that the electronic device executes the memory out-of-bounds location method as described above.

[0093] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein, for example. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0094] It should be understood that in the present application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.

[0095] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0096] In the embodiments of the present application, the term "module" or "unit" refers to a computer program or a part of a computer program that has a predetermined function and works together with other related parts to achieve a predetermined goal, and can be implemented in whole or in part by using software, hardware (such as processing circuits or memories), or a combination thereof. Similarly, a processor (or multiple processors or memories) can be used to implement one or more modules or units. In addition, each module or unit can be part of an overall module or unit that includes the function of the module or unit.

[0097] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0098] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0099] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, referred to as ROM), random access memory (Random Access Memory, referred to as RAM), disk or optical disk and other media that can store program codes.

[0100] The step numbers in the above method embodiment are only provided for the convenience of explanation and description, and no limitation is imposed on the order of the steps. The execution order of each step in the embodiment can be adaptively adjusted according to the understanding of those skilled in the art.

Claims

1. A memory out-of-bounds location method, characterized in that: The following steps are involved: Identify protected memory segments that have the potential for contamination; Allocating continuous memory blocks before and after the protected memory segment to obtain a memory protection zone, and calling a memory management unit to set the attribute of the memory protection zone to a read-only mode or a non-executable mode; Calling the memory management unit to respond to the CPU access request; When the memory management unit responds to the exception, it captures and locates the location where the memory out-of-bounds occurs.

2. The memory out-of-bounds location method according to claim 1, characterized in that: The step of identifying a protected memory segment that may be contaminated includes: Calling software analysis tools to monitor memory access patterns to determine memory access records before and after a memory cross-border event occurs; A memory segment with a high frequency of memory cross-boundary access is determined according to the memory access record, and the memory segment with a high frequency of memory cross-boundary access is determined as a protected memory segment.

3. The memory out-of-bounds location method according to claim 1, characterized in that: The step of identifying a protected memory segment that may be contaminated includes: Acquire the logic and data structure of the system service and determine the key memory area, where the key memory area is a memory segment containing an important data structure or a frequently accessed memory segment; The critical memory area is determined as a protected memory segment.

4. The memory out-of-bounds location method according to claim 1, characterized in that: The CPU access request generates a CPU-generated virtual address; The calling of the memory management unit to respond to the CPU access request includes: Acquire the virtual address, and send the virtual address to the memory management unit; The virtual address is converted into a physical address by the memory management unit.

5. The memory cross-border location method according to claim 4, characterized in that: The memory management unit responds to the exception including: The physical address access rights obtained by the memory management unit through conversion are insufficient; Alternatively, the memory management unit fails to convert the virtual address into a physical address.

6. The memory out-of-bounds location method according to claim 1, characterized in that: The capture and positioning of the location where the memory out-of-bounds occurs includes: Confirming that the memory management unit responds to an abnormal location in the protected memory segment, and obtaining stack information of the protected memory segment; According to the stack information, traverse the call stack using a stack backtrace library; Analyze the function addresses of each layer of the call stack and map the function addresses to source code files and line numbers; Print the source code file and the line number.

7. The memory out-of-bounds location method according to claim 1, characterized in that: When the memory management unit responds to the exception and captures and locates the location where the memory out-of-bounds occurs, the method further includes: Generate a location report, and send the location report to a log; The attributes of the protected memory segment are reset, and the memory management unit is called to respond to the CPU access request.

8. A memory out-of-bounds location device, characterized in that: include: A memory segment identification module, used to identify protected memory segments that may be contaminated; A protection allocation module, used for allocating continuous memory blocks before and after the protection memory segment to obtain a memory protection zone, and calling a memory management unit to set the attribute of the memory protection zone to a read-only mode or a non-executable mode; A request response module, used for calling the memory management unit to respond to the CPU access request; The location capture module is used to capture and locate the location where the memory crosses the boundary when the memory management unit responds to an exception.

9. An electronic device, characterized in that: include: at least one processor; at least one memory for storing at least one program; When at least one of the programs is executed by at least one of the processors, the method according to any one of claims 1 to 7 is implemented.

10. A computer-readable storage medium, characterized in that: A computer program executable by a processor is stored therein, and when the computer program executable by the processor is executed by the processor, it is used to implement the method according to any one of claims 1 to 7.

Citation Information

Cited By

  • Memory management method and computer equipment

    CN120704882A