Diagnostic positioning method, device and equipment for front and back border crossing of memory and medium
By configuring the dynamic link library path and setting buffers with inaccessible states, real-time detection and location of memory out-of-bounds without code modification and compilation environment support is achieved, and the limitations of memory out-of-bounds detection in the existing technology are solved. It is suitable for a variety of application scenarios and ensures the accuracy and performance of detection.
Patent Information
- Application Number
- CN202411388599.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-30
- Publication Date
- 2024-12-27
AI Technical Summary
The prior art is difficult to detect and locate memory out-of-bounds problems in real time without the support of code modification and compilation environment, and is suitable for a variety of application scenarios.
By configuring the dynamic link library path, the paths of the application calling the dynamic link library are kept consistent, receiving memory access instructions, and when executing memory access instructions, setting the front and back ends of the target memory to an inaccessible state, performing memory out-of-bounds detection, and generating Detection file carrying exception code location information.
It realizes real-time memory out-of-bounds detection and accurate positioning in various application scenarios without modifying user code and environment support, ensuring the determinism of dynamic memory usage of thread caches and minimizing performance losses.
Smart Images

Figure CN119201742A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of memory management technology, and in particular to a method, device, electronic device and computer-readable storage medium for diagnosing and locating memory front and back boundary violations. Background Art
[0002] In the related art, memory out-of-bounds refers to the situation where, after applying for memory, when the code operates a variable, a read or write operation is performed on the memory space outside the acquired memory address (including before and after the memory), that is, the reading or writing exceeds the allocated memory boundary.
[0003] There are two main solutions to the problem of memory out-of-bounds in the prior art. One is the memory mapping method, which is used by valgrind memcheck. However, this method needs to be run in the valgrind environment. If the error occurs in the customer environment, valgrind needs to be installed to analyze it, which limits the usage scenarios. The second is the filling method, most of which are based on simple character filling, such as oxCC, oxFF, etc., or, using key information filling, such as wmalloc, but the above two filling methods require modification of user code, and cannot detect and locate problem code in real time. In addition, ASAN uses filling method and memory mapping to realize memory out-of-bounds detection, but it needs to be combined with compile-time source code plug-in technology, which has high requirements and dependence on the compilation environment, and also requires environmental support during operation, which limits its usage scenarios.
[0004] Therefore, there is an urgent need for a memory cross-border diagnosis and positioning method that can perform real-time detection and positioning of memory cross-border without code modification and compilation environment support, and is suitable for a variety of application scenarios. Summary of the invention
[0005] In order to overcome the problems existing in the related art, the present disclosure provides a method, device, electronic device and computer-readable storage medium for diagnosing and locating memory front and back boundary crossings.
[0006] According to a first aspect of an embodiment of the present disclosure, a method for diagnosing and locating front and back memory boundary violations is provided, comprising: configuring a dynamic link library path so that the path of an application calling the dynamic link library remains consistent; receiving a memory access instruction, and determining a target memory corresponding to the memory access instruction based on the dynamic link library, wherein both the front and back ends of the target memory are provided with inaccessible buffers; calling a dynamic memory allocation function or a dynamic memory release function, executing the memory access instruction, and performing a memory boundary violation detection during the execution process, wherein the memory boundary violation detection includes a front memory boundary violation detection and a back memory boundary violation detection; and generating a detection file based on the memory boundary violation detection result, wherein the detection file carries the location information of the abnormal code.
[0007] In some embodiments, the receiving of memory access instructions and determining the target memory corresponding to the memory access instructions according to the dynamic link library include: receiving a memory access instruction, the memory access instruction including a memory application instruction and a memory release instruction; when the memory access instruction is a memory application instruction, obtaining a memory page size, and calculating the required number of memory pages according to the memory access instruction and the memory page size; based on the number of memory pages, applying for page boundary-aligned continuous memory using a dynamic memory allocation function to obtain the applied memory size; initializing the applied continuous memory and forming a thread cache through indexing; determining the corresponding target memory in the thread cache according to the memory access instruction, and setting inaccessible buffers at the front and back ends of the target memory.
[0008] In some embodiments, when a dynamic memory allocation function is called, the memory access instruction is executed, and a memory out-of-bounds check is performed during the execution process, it includes: searching for the corresponding free memory page according to the thread cache index, using the free memory page head address as the target memory, and setting the free memory page head address to an inaccessible state; calculating the memory application head address according to the free memory page head address and the memory page size, and returning it.
[0009] In some embodiments, when a dynamic memory allocation function is called, the memory access instruction is executed, and a memory post-bounds detection is performed during the execution process, it includes: searching for the corresponding free memory page according to the thread cache index as the target memory, and calculating the memory application first address according to the free memory page first address, the memory page size and the applied memory size; calculating the first address of the buffer zone according to the free memory page first address, the memory page size and the applied memory size; setting the first address of the buffer zone to an inaccessible state, and returning the memory application first address.
[0010] In some embodiments, when a dynamic memory release function is called, the memory access instruction is executed, and a memory out-of-bounds detection is performed during the execution process, it includes: adjusting the free memory page head address from an inaccessible state to an accessible state; and releasing the free memory page corresponding to the target memory to the thread cache.
[0011] In some embodiments, when a dynamic memory release function is called, the memory access instruction is executed, and a memory out-of-bounds detection is performed during the execution process, it includes: calculating the last page header address of the target memory based on the free memory page header address and the requested memory size; adjusting the last page header address from an inaccessible state to an accessible state, and releasing the last page of memory to the thread cache.
[0012] In some embodiments, generating a detection file based on a memory out-of-bounds detection result includes: obtaining a memory out-of-bounds detection result based on a memory out-of-bounds detection, and reading the memory out-of-bounds detection result; when the code of the buffer is read, indicating that a memory out-of-bounds occurs, an interrupt signal is generated, a call stack is formed as a detection file, and the call stack can locate the abnormal code; when the code of the buffer is not read, indicating that no memory out-of-bounds occurs, a blank detection file is generated.
[0013] According to a second aspect of an embodiment of the present disclosure, a diagnostic positioning device for front and back memory boundary violations is provided, comprising: a dynamic link library configuration module, used to configure the dynamic link library path so that the path of the application calling the dynamic link library remains consistent; a target memory determination module, used to receive a memory access instruction, and determine the target memory corresponding to the memory access instruction according to the dynamic link library, wherein both the front and back ends of the target memory are provided with inaccessible buffers; a memory boundary violation detection module, used to call a dynamic memory allocation function or a dynamic memory release function, execute the memory access instruction, and perform memory boundary violation detection during the execution process, wherein the memory boundary violation detection includes front memory boundary violation detection and back memory boundary violation detection; a detection file generation module, used to generate a detection file according to the memory boundary violation detection result, wherein the detection file carries the location information of the abnormal code.
[0014] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing executable instructions of the processor; the processor is used to read the executable instructions from the memory and execute the instructions to implement the diagnostic positioning method for front and back memory boundary violations provided by the first aspect of the present disclosure.
[0015] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided, on which computer program instructions are stored. When the program instructions are executed by a processor, the steps of the method for diagnosing and locating memory front and back boundary violations provided by the first aspect of the present disclosure are implemented.
[0016] The technical solution provided by the embodiments of the present disclosure may include the following beneficial effects: by configuring the dynamic link library path, the path of the application calling the dynamic link library is kept consistent, a memory access instruction is received, and the target memory corresponding to the memory access instruction is determined according to the dynamic link library, and an inaccessible buffer is set at both ends of the target memory; a dynamic memory allocation function or a dynamic memory release function is called to execute the memory access instruction, and a memory out-of-bounds detection is performed during the execution process, and the memory out-of-bounds detection includes a pre-memory out-of-bounds detection and a post-memory out-of-bounds detection. According to the memory out-of-bounds detection result, a detection file is generated, and the detection file carries the location information of the abnormal code, thereby ensuring the determinism of the use of the thread cache dynamic memory, and can adopt the filling technology with the least performance loss without modifying the user code and environmental support, so as to realize the real-time detection of the dynamic memory out-of-bounds problem of the application in a variety of application scenarios, and accurately locate the abnormal code in real time.
[0017] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0019] Figure 1 The present invention is a flowchart of a method for diagnosing and locating memory front and back boundary violations according to an exemplary embodiment.
[0020] Figure 2 A dynamic memory filling method is shown according to an exemplary embodiment.
[0021] Figure 3 The figure is a flowchart of a method for determining a target memory according to an exemplary embodiment.
[0022] Figure 4 The present invention is a flowchart of a method for performing memory cross-bounds detection before calling a memory allocation function according to an exemplary embodiment.
[0023] Figure 5 The present invention is a flowchart of memory front-end and back-end cross-bounds detection under dynamic memory allocation or release function call according to an exemplary embodiment.
[0024] Figure 6 The present invention is a flowchart of a method for performing memory cross-bounds detection after calling a memory allocation function according to an exemplary embodiment.
[0025] Figure 7 The present invention is a flowchart of a method for performing memory cross-bounds detection before calling a memory release function according to an exemplary embodiment.
[0026] Figure 8 The present invention is a flowchart of a method for performing memory post-bounds detection after calling a memory release function according to an exemplary embodiment.
[0027] Fig. 9 The figure is a flowchart of a method for generating a detection file according to an exemplary embodiment.
[0028] Fig.10 The invention is a block diagram showing a device for diagnosing and locating memory front and back boundary violations according to an exemplary embodiment.
[0029] Fig.11 It is a block diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION
[0030] Exemplary embodiments will be described in detail below with reference to the accompanying drawings.
[0031] It should be pointed out that the relevant embodiments and drawings are only for describing exemplary embodiments provided by the present disclosure, rather than all embodiments of the present disclosure, and it should not be understood that the present disclosure is limited to the relevant exemplary embodiments.
[0032] It should be noted that the terms "first", "second", etc. used in the present disclosure are only used to distinguish different steps, devices or modules, etc. The related terms neither represent any specific technical meanings nor indicate the order or interdependence between them.
[0033] It should be noted that the modifications of the terms "one", "multiple", and "at least one" used in the present disclosure are illustrative rather than restrictive. Unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0034] It should be noted that the term "and / or" used in this disclosure is used to describe the association relationship between associated objects, and generally indicates that there are at least three association relationships. For example, A and / or B can at least indicate the existence of three association relationships: A exists alone, A and B exist at the same time, and B exists alone.
[0035] It should be noted that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. Unless otherwise specified, the scope of the present disclosure is not limited by the order of description of the steps in the relevant embodiments.
[0036] It should be noted that all actions of acquiring signals, information or data in the present disclosure are carried out in compliance with the relevant data protection laws and policies of the country where the device is located and with the authorization given by the owner of the corresponding device.
[0037] Exemplary Methods
[0038] Figure 1 is a flow chart of a method for diagnosing and locating memory front and back boundary violations according to an exemplary embodiment. Figure 1 As shown, the memory front and back boundary crossing diagnosis and positioning method is used in memory management, including the following steps:
[0039] In step S110, the dynamic link library path is configured so that the path used by the application to call the dynamic link library remains consistent.
[0040] In some embodiments, dead page filling is used to configure the application's environment variable export LD_PRELOAD without modifying the user code to generate a dynamic link library path so that the path used by the application to call the dynamic link library remains consistent, thereby improving the compatibility, stability, and maintainability of the application.
[0041] In step S120, a memory access instruction is received, and a target memory corresponding to the memory access instruction is determined according to a dynamic link library, and buffers in an inaccessible state are provided at both ends of the target memory.
[0042] In some embodiments, when memory access is required, a memory access instruction is issued, the memory access instruction includes a memory application instruction and a memory release instruction, the issued memory access instruction is received, and the target memory corresponding to the memory access instruction is determined according to the dynamic link library, the target memory is a Live page, so as to allocate or release the target memory, and the front and back ends of the target memory are both provided with an inaccessible cache area, that is, a Dead page, which can be called through the mprotect system, for example:
[0043] bool set_dead_page(void*deadPageAddr,size_t pageSize){
[0044] return mprotect((char*)deadPageAddr,pageSize,PROT_NONE)===0;
[0045] }
[0046] Similarly, Live page is set to be rewritable. Among them, the mprotect system call is suitable for Linux and Unix operating systems, and the Windows operating system can be implemented through the VirtualProtect system call.
[0047] In step S130, a dynamic memory allocation function or a dynamic memory release function is called to execute a memory access instruction, and a memory cross-boundary detection is performed during the execution process. The memory cross-boundary detection includes a pre-memory cross-boundary detection and a post-memory cross-boundary detection.
[0048] In some embodiments, after determining the target memory, a dynamic memory allocation function or a dynamic memory release function is called according to a memory access instruction, and the memory access instruction is executed to allocate or release the target memory. During the execution process, a memory out-of-bounds detection is performed to determine whether there is a read or write operation on the memory space before or after the acquired target memory address, thereby realizing pre-memory out-of-bounds detection and post-memory out-of-bounds detection during the function execution.
[0049] In actual application, the front cross-border or back cross-border detection can be performed separately to realize real-time and accurate detection of front and back cross-border of memory. The back cross-border detection method is usually adopted in the application program. By running the back cross-border detection, the memory cross-border detection can be realized.
[0050] In step S140, a detection file is generated according to the memory out-of-bounds detection result, and the detection file carries the location information of the abnormal code.
[0051] In some embodiments, after performing pre-memory out-of-bounds detection or post-memory out-of-bounds detection, a corresponding detection result is obtained, and a detection file is generated based on the memory out-of-bounds detection result. The detection file carries the location information of the abnormal code, thereby achieving accurate and real-time positioning of the memory out-of-bounds detection and its problem code. It is easy to customize and can be applied to a variety of application scenarios without relying on environmental support or code modification.
[0052] In the above embodiment, by configuring the dynamic link library path, the path of the application calling the dynamic link library is kept consistent, a memory access instruction is received, and the target memory corresponding to the memory access instruction is determined according to the dynamic link library, and inaccessible buffers are set at both ends of the target memory; the dynamic memory allocation function or the dynamic memory release function is called to execute the memory access instruction, and a memory out-of-bounds detection is performed during the execution process. The memory out-of-bounds detection includes a pre-memory out-of-bounds detection and a post-memory out-of-bounds detection. According to the memory out-of-bounds detection result, a detection file is generated. The detection file carries the location information of the abnormal code to ensure the certainty of the use of the thread cache dynamic memory. Without modifying the user code and environmental support, the filling technology with the least performance loss can be adopted to realize the real-time detection of the dynamic memory out-of-bounds problem of the application in a variety of application scenarios, and the abnormal code can be accurately located in real time.
[0053] Among them, for special customer on-site operation, the user APP does not need to make any changes, and only needs to configure the environment variable export LD_PRELOAD = the dynamic library path generated by this patent to achieve it. Combined with the deterministic implementation of C / C++ dynamic memory management, the memory out-of-bounds check can be well implemented through configuration.
[0054] Among them, the dynamic memory filling method is as follows Figure 2 As shown in the figure, the boxes where the Live page and Dead page are located represent a memory page respectively, with a default size of 4KB. The memory space used by the user is the memory space dynamically requested by the user. In the example, the size is smaller than one page. If it is larger than one page, it can be expanded in the same way. The memory page where the memory space used by the user adjacent to the Dead page is located is the Live page.
[0055] Figure 3 is a flow chart of a method for determining a target memory according to an exemplary embodiment. Figure 3 As shown, the target memory determination method is used in step S120, comprising the following steps:
[0056] In step S210, a memory access instruction is received, where the memory access instruction includes a memory application instruction and a memory release instruction.
[0057] Specifically, when memory application or memory release is required, a memory application instruction or a memory release instruction is issued, and the memory access instruction is received to perform memory application or memory release.
[0058] In step S220, when the memory access instruction is a memory request instruction, the memory page size is obtained, and the required number of memory pages is calculated according to the memory access instruction and the memory page size.
[0059] Specifically, when the memory access instruction is a memory application instruction, the memory page size page_size is obtained through sysconf(_SC_PAGE_SIZE), and the required number of memory pages is calculated according to the memory access instruction and the memory page size. The formula is: block number*((malloc size+9) / page_size+1)+1.
[0060] In step S230, based on the number of memory pages, a dynamic memory allocation function is used to apply for continuous memory aligned with page boundaries to obtain the applied memory size.
[0061] Specifically, based on the number of memory pages, a dynamic memory allocation function, such as the posix_memalign function, is used to apply for page boundary-aligned continuous memory to obtain the applied memory size, so as to improve memory access efficiency and reduce performance loss, thereby ensuring data correctness and efficiency.
[0062] In step S240, the applied continuous memory is initialized and a thread cache is formed through indexing.
[0063] Specifically, apply for continuous memory aligned with page boundaries, initialize the applied continuous memory, form a thread cache through indexing, and through the thread cache, after determining the required memory size, perform the same allocation of related memory. After releasing the memory, the released memory can also be managed uniformly to achieve management of applied memory and released memory.
[0064] In step S250, the corresponding target memory is determined in the thread cache according to the memory access instruction, and inaccessible cache areas are set at the front and rear ends of the target memory.
[0065] Specifically, when applying for memory, the corresponding target memory is determined in the thread cache according to the memory access instruction, and inaccessible cache dead pages are set at the front and back ends of the target memory. The memory page where the memory space used by users adjacent to the dead page is located is the accessible live page.
[0066] Through the above steps, when the Thread Cache is initialized, the Deadpage setting can be implemented based on the detection of the front and back cross-borders, and when the Thread Cache exits, the Dead page is set to the Live page.
[0067] In this embodiment, by receiving memory access instructions, including memory application instructions and memory release instructions, when the memory access instruction is a memory application instruction, the memory page size is obtained, and the required number of memory pages is calculated according to the memory access instruction and the memory page size; based on the number of memory pages, a dynamic memory allocation function is used to apply for page boundary-aligned continuous memory to obtain the applied memory size; the applied continuous memory is initialized, and a thread cache is formed through indexing to facilitate the management of applied memory and released memory, and the corresponding target memory is determined in the thread cache according to the memory access instruction, and an inaccessible buffer is set at the front and back ends of the target memory to achieve the determination of the target memory and the setting of the buffer, so as to facilitate the front and back memory cross-border detection by filling the buffer, and no code adjustment is required.
[0068] Figure 4 is a flowchart of a method for performing a memory cross-border detection before calling a memory allocation function according to an exemplary embodiment. Figure 4 As shown, the method for performing memory cross-bounds detection before calling a memory allocation function is used in step S130, and includes the following steps:
[0069] In step S310, the corresponding free memory page is searched according to the thread cache index as the target memory, and the head address of the free memory page is set to an inaccessible state.
[0070] Specifically, when calling a dynamic memory allocation function to perform a pre-memory cross-bounds check, such as Figure 5 As shown, the memory out-of-bounds detection during DDS runtime is taken as an example. It should be noted that this patent is not limited to DDS applications, and other programs can also be extended for use. According to the applied thread cache continuous pages memory, the DDS malloc function is called to determine whether the current operation is a front out-of-bounds detection. If it is a front out-of-bounds detection of memory, the corresponding free memory page is searched according to the thread cache thread cache index, and it is used as the target memory, and the free memory page head address is set to a dead page in an inaccessible state.
[0071] In step S320, the memory application first address is calculated according to the free memory page first address and the memory page size, and returned.
[0072] Specifically, the memory application first address is calculated based on the free memory page first address plus the memory page size PAGE_SIZE, and returned to the system interface. The DDS malloc function ends, and the memory front cross-bounds detection is implemented under the dynamic memory allocation function call.
[0073] The above steps can also be used to perform deterministic allocation of dynamic memory and to calculate and initialize dynamic memory.
[0074] In this embodiment, by calling the dynamic memory allocation function, it is determined whether the current operation is a front cross-border detection. If it is a front cross-border detection of memory, the corresponding free memory page is searched according to the thread cache index as the target memory, and the head address of the free memory page is set to a dead page in an inaccessible state. The memory application head address is calculated and returned according to the head address of the free memory page and the memory page size, and the front cross-border detection result under the dynamic memory allocation function call is obtained. The thread cache is used to manage the free memory pages, so that the thread cache can adapt to various application scenarios and realize real-time and accurate detection of front cross-border detection of memory.
[0075] Figure 6 is a flowchart of a method for performing a memory cross-border detection after calling a memory allocation function according to an exemplary embodiment. Figure 6 As shown, the method for detecting memory cross-bounds after calling the memory allocation function is used in step S130, and includes the following steps:
[0076] In step S410, the corresponding free memory page is searched according to the thread cache index as the target memory, and the memory application first address is calculated according to the free memory page first address, the memory page size and the application memory size.
[0077] Specifically, when calling a dynamic memory allocation function to perform a memory out-of-bounds check, such as Figure 5 As shown in the figure, take the memory out-of-bounds detection during DDS runtime as an example. According to the applied thread cache continuous pages memory, call the DDS malloc function to determine whether the current run is a front out-of-bounds detection. If it is a back out-of-bounds detection, find the corresponding free memory page according to the thread cache threadcache index as the target memory, calculate the sum of the memory page head address and (PAGE_SIZE-applied memory size%page_size), get the memory application head address and return it.
[0078] In step S420, the first address of the cache area is calculated according to the first address of the free memory page, the memory page size and the requested memory size.
[0079] Specifically, according to the free memory page head address, memory page size and applied memory size, the first address of the buffer dead page is calculated as follows: memory page head address + PAGE_SIZE * (applied memory size / page_size + 1).
[0080] In step S430, the first address of the buffer is set to an inaccessible state, and the first address of the memory application is returned.
[0081] Specifically, after calculating the first address of the buffer, it is set to an inaccessible state, and the first address of the memory application is returned, completing the memory back-out-of-bounds detection under the call of the dynamic memory allocation function, and realizing real-time and accurate detection of memory back-out-of-bounds.
[0082] In this embodiment, by calling the dynamic memory allocation function, it is determined whether the current operation is a front out-of-bounds detection. If it is a back out-of-bounds memory detection, the corresponding free memory page is searched according to the thread index as the target memory. The memory application first address is calculated according to the free memory page first address, the memory page size and the applied memory size. The first address of the buffer is calculated according to the free memory page first address, the memory page size and the applied memory size. The first address of the buffer is set to an inaccessible state, the memory application first address is returned, and the back out-of-bounds detection result under the dynamic memory allocation function call is obtained. The free memory pages are managed by thread cache, so that various application scenarios can be adapted through thread cache to achieve real-time and accurate detection of back out-of-bounds memory.
[0083] Figure 7is a flowchart of a method for performing a memory cross-border detection before calling a memory release function according to an exemplary embodiment. Figure 7 As shown, the method for calling the memory release function to perform memory cross-bounds detection is used in step S130, including the following steps:
[0084] In step S510, the free memory page head address is adjusted from an inaccessible state to an accessible state.
[0085] Specifically, when calling the dynamic memory release function to perform memory back-end cross-bounds detection, such as Figure 5 As shown in the figure, take the memory out-of-bounds detection during DDS runtime as an example. According to the applied thread cache continuous pages memory, the DDS free function is called to determine whether the current run is a front out-of-bounds detection. If it is a front out-of-bounds detection, the free memory page head address is adjusted from an inaccessible state to an accessible state, and the DDS free head address sets the dead page to the live page to facilitate access to the free memory page.
[0086] In step S520, the free memory pages corresponding to the target memory are released to the thread cache.
[0087] Specifically, after the target memory is adjusted to an accessible state, the free memory page corresponding to the target memory is released to the thread cache, the released memory is managed by the thread cache, and the calling of the memory release function is terminated.
[0088] In this embodiment, by calling the dynamic memory release function, it is determined whether the current operation is a front cross-border detection. If it is a front cross-border detection of memory, the head address of the free memory page is adjusted from an inaccessible state to an accessible state, and the free memory page corresponding to the target memory to be released is released to the thread cache, and the front cross-border detection result under the dynamic memory release function call is obtained. The thread cache is used to manage the released memory, so that the thread cache can adapt to various application scenarios and realize real-time and accurate detection of front cross-border detection of memory.
[0089] Figure 8 is a flowchart of a method for performing memory cross-border detection after calling a memory release function according to an exemplary embodiment. Figure 8 As shown, the method for calling the memory release function to perform memory cross-bounds detection is used in step S130, and includes the following steps:
[0090] In step S610, the last page header address of the target memory is calculated according to the free memory page header address and the requested memory size.
[0091] Specifically, when calling the dynamic memory release function to perform memory back-end cross-bounds detection, such as Figure 5As shown in the figure, take the memory out-of-bounds detection during DDS runtime as an example. According to the applied thread cache continuous pages memory, call the DDS free function to determine whether the current run is a front out-of-bounds detection. If it is a back out-of-bounds detection, calculate the last page head address of the target memory based on the free memory page head address and the applied memory size. The formula is: the last page head address = the first address of the free function + the size of the applied memory obtained by the index in the thread cache, and perform a back out-of-bounds detection based on the last page of memory.
[0092] In step S620 , the last page header address is adjusted from an inaccessible state to an accessible state, and the last page memory is released to the thread cache.
[0093] Specifically, the last page head address is adjusted from an inaccessible state to an accessible state, and the last page of memory is released to a released memory data structure of the thread cache, thereby implementing the call of the dynamic memory release function.
[0094] In this embodiment, by calling the dynamic memory release function, it is determined whether the current operation is a front out-of-bounds detection. If it is a back out-of-bounds memory, the last page header address of the target memory is calculated based on the free memory page header address and the requested memory size, and the last page header address is adjusted from an inaccessible state to an accessible state. The last page of memory is released to the thread cache, and the back out-of-bounds detection result under the dynamic memory release function call is obtained. The thread cache is used to manage the released memory, so that the thread cache can adapt to various application scenarios and realize real-time and accurate detection of back out-of-bounds memory.
[0095] Fig. 9 is a flow chart of a method for generating a detection file according to an exemplary embodiment. Fig. 9 As shown, the detection file generation method is used in step S140, comprising the following steps:
[0096] In step S710, a memory out-of-bounds detection result is obtained according to the memory out-of-bounds detection, and the memory out-of-bounds detection result is read.
[0097] Specifically, based on the aforementioned memory out-of-bounds detection, a corresponding memory out-of-bounds detection result is obtained. The memory out-of-bounds detection result can be the memory out-of-bounds detection result before and after the dynamic memory function call, or the memory out-of-bounds detection result before and after the dynamic memory release function call. The memory out-of-bounds detection result is read to judge the memory out-of-bounds situation according to the read result.
[0098] In step S720, when the code in the buffer is read, it indicates that a memory out-of-bounds occurs, an interrupt signal is generated, a call stack is formed as a detection file, and the call stack can locate the abnormal code.
[0099] Specifically, the memory out-of-bounds detection result is read. When the buffer code is read therein, it indicates that a memory out-of-bounds condition exists. The kernel generates a SIGSEGV interrupt signal, the DDS application crashes, and a call stack is formed as a detection file. The call stack can locate the abnormal code and locate the code that causes the memory out-of-bounds condition, so that the developer can handle the abnormal code in a timely manner.
[0100] In step S730, when the code in the buffer is not read, it indicates that no memory boundary crossing occurs, and a blank detection file is generated.
[0101] Specifically, when the memory out-of-bounds detection result is read and no buffer code is read, it indicates that the program runs normally and no memory out-of-bounds situation occurs, a blank detection file is generated, and the program continues to run normally.
[0102] In this embodiment, a memory out-of-bounds detection result is obtained based on the memory out-of-bounds detection, and the memory out-of-bounds detection result is read. When the buffer code is read, it indicates that a memory out-of-bounds has occurred, an interrupt signal is generated, and a call stack is formed as a detection file. The call stack can be used to locate the abnormal code, so that the developer can process the abnormal code in time. When the buffer code is not read, it indicates that no memory out-of-bounds has occurred, and a blank detection file is generated to facilitate the program to continue running, thereby detecting and locating the memory out-of-bounds before and after the function call, and the detection result is accurate and timely.
[0103] Based on the above steps, when the thread cache is initialized, the dead page can be set according to the detection of the front and back cross-border. When the thread cache is exited, the dead page can be set to the live page. In actual use, you only need to use conditional compilation or runtime switch control of the thread cache to adapt to the application of DDS scenarios that are not suitable for thread cache.
[0104] Exemplary Devices
[0105] Fig.10 The block diagram of a device for diagnosing and locating memory boundary crossings is shown according to an exemplary embodiment. Fig.10 The device 800 includes a dynamic link library configuration module 810, a target memory determination module 820, a memory cross-border detection module 830 and a detection file generation module.
[0106] The dynamic link library configuration module 810 is used to configure the dynamic link library path so that the path used by the application to call the dynamic link library remains consistent;
[0107] The target memory determination module 820 is used to receive a memory access instruction and determine a target memory corresponding to the memory access instruction according to a dynamic link library, wherein both ends of the target memory are provided with buffers in an inaccessible state;
[0108] A memory cross-bounds detection module 830 is used to call a dynamic memory allocation function or a dynamic memory release function, execute a memory access instruction, and perform a memory cross-bounds detection during the execution process. The memory cross-bounds detection includes a memory front cross-bounds detection and a memory back cross-bounds detection.
[0109] The detection file generation module 840 is used to generate a detection file according to the memory cross-boundary detection result, and the detection file carries the location information of the abnormal code.
[0110] In one embodiment, the device also includes: an access instruction receiving module, which is used to receive memory access instructions, and the memory access instructions include memory application instructions and memory release instructions; a memory page number calculation module, which is used to obtain the memory page size when the memory access instruction is a memory application instruction, and calculate the required number of memory pages according to the memory access instruction and the memory page size; a memory application module, which is used to apply for page boundary-aligned continuous memory based on the number of memory pages using a dynamic memory allocation function to obtain the applied memory size; a thread cache formation module, which is used to initialize the applied continuous memory and form a thread cache through an index; a target memory determination module, which is used to determine the corresponding target memory in the thread cache according to the memory access instruction, and set an inaccessible buffer zone at the front and back ends of the target memory.
[0111] In one embodiment, the device also includes: a free memory page search module, which is used to search for the corresponding free memory page according to the thread cache index as the target memory, and set the free memory page head address to an inaccessible state; a memory application first address calculation module, which is used to calculate the memory application first address according to the free memory page head address and the memory page size, and return it.
[0112] In one embodiment, the device also includes: a target memory search module, which is used to search for the corresponding free memory page according to the thread cache index as the target memory, and calculate the memory application first address according to the free memory page first address, the memory page size and the application memory size; a buffer first address calculation module, which is used to calculate the buffer first address according to the free memory page first address, the memory page size and the application memory size; a first address setting module, which is used to set the buffer first address to an inaccessible state and return the memory application first address.
[0113] In one embodiment, the device further includes: an access state adjustment module, used to adjust the free memory page head address from an inaccessible state to an accessible state; and a target memory release module, used to release the free memory page corresponding to the target memory to the thread cache.
[0114] In one embodiment, the device also includes: a last page header address calculation module, which is used to calculate the last page header address of the target memory based on the free memory page header address and the requested memory size; a memory release module, which is used to adjust the last page header address from an inaccessible state to an accessible state, and release the last page of memory to the thread cache.
[0115] In one embodiment, the device also includes: a detection result reading module, which is used to obtain a memory out-of-bounds detection result based on a memory out-of-bounds detection, and read the memory out-of-bounds detection result; a memory out-of-bounds processing module, which is used to generate an interrupt signal when a code in a buffer is read, indicating that a memory out-of-bounds occurs, and form a call stack as a detection file, and the call stack can locate the abnormal code; a blank detection file generation module, which is used to generate a blank detection file when no code in the buffer is read, indicating that no memory out-of-bounds occurs.
[0116] In the above embodiment, through the operation of the device, a diagnostic positioning method based on front and back boundary crossing of memory is implemented, ensuring that the device can implement all contents of a diagnostic positioning method based on front and back boundary crossing of memory.
[0117] Exemplary Electronic Devices
[0118] Fig.11 1 is a block diagram of an electronic device 900 according to an exemplary embodiment. The electronic device 900 may be a vehicle controller, a vehicle terminal, a vehicle computer or other types of electronic devices.
[0119] Reference Fig.11 , the electronic device 900 may include at least one processor 910 and a memory 920. The processor 910 may execute instructions stored in the memory 920. The processor 910 is communicatively connected to the memory 920 via a data bus. In addition to the memory 920, the processor 910 may also be communicatively connected to an input device 930, an output device 940, and a communication device 950 via a data bus.
[0120] The processor 910 may be any conventional processor, such as a commercially available CPU. The processor may also include a graphics processor (Graphic Process Unit, GPU), a field programmable gate array (Field Programmable Gate Array, FPGA), a system on chip (System on Chip, SOC), an application specific integrated circuit (Application Specific Integrated Circuit, ASIC) or a combination thereof.
[0121] The memory 920 may be implemented by any type of volatile or nonvolatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0122] In an embodiment of the present disclosure, executable instructions are stored in the memory 920, and the processor 910 can read the executable instructions from the memory 920 and execute the instructions to implement all or part of the steps of the diagnostic positioning method for front and back memory boundary violations described in any of the above exemplary embodiments.
[0123] Exemplary computer-readable storage media
[0124] In addition to the above methods and devices, the exemplary embodiments of the present disclosure may also be a computer program product or a computer-readable storage medium storing the computer program product. The computer product includes computer program instructions that can be executed by a processor to implement all or part of the steps described in any method in the above exemplary embodiments.
[0125] The computer program product may be written in any combination of one or more programming languages to write program codes for performing the operations of the embodiments of the present application, including object-oriented programming languages, such as Java, C++, etc., and also conventional procedural programming languages, such as "C" language or similar programming languages and scripting languages (e.g., Python). The program code may be executed entirely on the user computing device, partially on the user computing device, as an independent software package, partially on the user computing device and partially on the remote computing device, or entirely on the remote computing device or server.
[0126] The computer readable storage medium can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can include, for example, but is not limited to, a system, device or device of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination of the above. More specific examples of readable storage media include: a static random access memory (SRAM) with one or more wires electrically connected, an electrically erasable programmable read-only memory (EEPROM), an erasable programmable read-only memory (EPROM), a programmable read-only memory (PROM), a read-only memory (ROM), a magnetic storage, a flash memory, a magnetic disk or an optical disk, or any suitable combination of the above.
[0127] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the present disclosure. This application is intended to cover any variations, uses or adaptations of the present disclosure, which follow the general principles of the present disclosure and include common knowledge or customary technical means in the art that are not disclosed in the present disclosure. The specification and examples are to be considered as exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0128] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A method for diagnosing and locating memory front and back boundary violations, characterized in that: include: Configure the dynamic link library path to ensure that the path used by the application to call the dynamic link library remains consistent; Receive a memory access instruction, and determine a target memory corresponding to the memory access instruction according to the dynamic link library, wherein both ends of the target memory are provided with buffers in an inaccessible state; Calling a dynamic memory allocation function or a dynamic memory release function, executing the memory access instruction, and performing a memory cross-boundary detection during the execution process, wherein the memory cross-boundary detection includes a memory front cross-boundary detection and a memory back cross-boundary detection; A detection file is generated according to the memory cross-boundary detection result, and the detection file carries the location information of the abnormal code.
2. The method for diagnosing and locating memory front and back boundary violations according to claim 1, characterized in that: The receiving of the memory access instruction and determining the target memory corresponding to the memory access instruction according to the dynamic link library includes: Receiving a memory access instruction, wherein the memory access instruction includes a memory application instruction and a memory release instruction; When the memory access instruction is a memory application instruction, obtaining a memory page size, and calculating the required number of memory pages according to the memory access instruction and the memory page size; Based on the number of memory pages, a dynamic memory allocation function is used to apply for continuous memory aligned with page boundaries to obtain an applied memory size; Initialize the requested continuous memory and form a thread cache through indexing; The corresponding target memory is determined in the thread cache according to the memory access instruction, and buffers in an inaccessible state are set at the front and rear ends of the target memory.
3. The method for diagnosing and locating memory front and back boundary violations according to claim 2, characterized in that: When the dynamic memory allocation function is called, the memory access instruction is executed, and a memory front cross-bounds detection is performed during the execution, including: Find the corresponding free memory page according to the thread cache index as the target memory, and set the free memory page head address to an inaccessible state; The memory application first address is calculated according to the free memory page first address and the memory page size, and returned.
4. The method for diagnosing and locating memory front and back boundary violations according to claim 2, characterized in that: When the dynamic memory allocation function is called, the memory access instruction is executed, and a memory back-out-of-bounds detection is performed during the execution, including: Find the corresponding free memory page according to the thread cache index as the target memory, and calculate the memory application first address according to the free memory page first address, memory page size and application memory size; The first address of the buffer is calculated according to the first address of the free memory page, the memory page size and the requested memory size; The first address of the buffer is set to an inaccessible state, and the first address of the memory application is returned.
5. The method for diagnosing and locating memory front and back boundary violations according to claim 3, characterized in that: When the dynamic memory release function is called, the memory access instruction is executed, and a memory front cross-bounds detection is performed during the execution, including: Adjusting the free memory page header address from an inaccessible state to an accessible state; Release the free memory pages corresponding to the target memory to the thread cache.
6. The method for diagnosing and locating memory front and back boundary violations according to claim 4, characterized in that: When the dynamic memory release function is called, the memory access instruction is executed, and a memory back-out-of-bounds detection is performed during the execution, including: Calculate the last page header address of the target memory according to the free memory page header address and the requested memory size; The last page header address is adjusted from an inaccessible state to an accessible state, and the last page memory is released to the thread cache.
7. The method for diagnosing and locating memory front and back boundary violations according to claim 1, characterized in that: The generating of the detection file according to the memory cross-bounds detection result includes: Obtaining a memory cross-boundary detection result according to the memory cross-boundary detection, and reading the memory cross-boundary detection result; When the code in the buffer is read, it indicates that a memory out-of-bounds occurs, an interrupt signal is generated, a call stack is formed as a detection file, and the call stack can locate the abnormal code; When the code in the buffer is not read, it indicates that no memory cross-border occurs, and a blank detection file is generated.
8. A diagnostic positioning device for memory front and back boundary crossing, characterized in that: include: The dynamic link library configuration module is used to configure the dynamic link library path so that the path used by the application to call the dynamic link library remains consistent; A target memory determination module, used for receiving a memory access instruction, and determining a target memory corresponding to the memory access instruction according to the dynamic link library, wherein both ends of the target memory are provided with buffers in an inaccessible state; A memory cross-bounds detection module is used to call a dynamic memory allocation function or a dynamic memory release function, execute the memory access instruction, and perform memory cross-bounds detection during the execution process, wherein the memory cross-bounds detection includes a pre-memory cross-bounds detection and a post-memory cross-bounds detection; The detection file generation module is used to generate a detection file according to the memory cross-boundary detection result, and the detection file carries the location information of the abnormal code.
9. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the diagnostic positioning method for front and back memory boundary violations as described in any one of claims 1-7.
10. A computer-readable storage medium having computer program instructions stored thereon, characterized in that: When the program instructions are executed by a processor, the steps of the method for diagnosing and locating memory front and back boundary violations described in any one of claims 1-7 are implemented.