Memory detection method and computer system
By dividing the memory area and coding area in the memory space of the embedded device, and using the alignment red zone and read-write indicator unit, efficient memory detection is achieved, solving the problems of poor real-time memory detection and large resource consumption in the prior art.
Patent Information
- Application Number
- CN202010270651.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-04-08
- Publication Date
- 2025-05-27
- Estimated Expiration
- 2040-04-08
AI Technical Summary
When the prior art realizes memory detection in embedded devices, the real-time performance is poor, it is difficult to accurately locate the problem, and the resource consumption is large, making it difficult to apply in embedded devices with resource-constrained.
By dividing the memory area and the encoding area in the memory space, memory detection is realized using the alignment red area and the read-write indication unit. The specific method includes allocating storage space for variables in the storage area and then allocating an unreadable and unwritable alignment red zone to detect before accessing memory.
This method improves memory detection efficiency and accuracy by reducing the space occupied by the coding area, and reduces the consumption of resources, making it suitable for resource-constrained embedded devices.
Smart Images

Figure CN113495814B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technologies, and particularly relates to a memory detection method and a computer device based on this method, etc. Background Art
[0002] With the rapid development of the Internet of Things (IOT) industry, embedded devices applied to IOT devices have become the mainstream R & D products in the industry. However, since most embedded devices are developed using the C language, there are significant challenges in terms of memory security and reliability. Due to programmers' incorrect coding, or issues such as porting and debugging, various memory security problems can occur: memory safety-related problems such as buffer over flow (BOF), use after free (UAF), double free, and read / write at zero address. The losses caused by these problems can be huge.
[0003] A commonly used technique for detecting memory in modern operating system kernels is to add a magic word after the allocated memory. When an overflow occurs, the magic word may be modified. After the memory space is released, the integrity of the magic word will be detected. If it is detected that the magic word has been modified, it can be determined that a memory problem has occurred. This traditional memory detection method has very poor real-time performance and it is difficult to accurately locate where the problem occurred. And there is a high probability that it can be bypassed by attackers.
[0004] In such a background, two major types of methods have emerged for real-time detection of access addresses. One is to perform system simulation by simulating the operation of the CPU and detect the address at the simulation level. The other is to insert function interfaces before read / write instructions through the method of instrumentation to detect the legality of the address to be accessed. The existing implementations of the above methods are all based on the Linux or Windows system platforms with rich memory resources. The existing solutions consume a huge amount of memory system resources, which are unbearable for resource-constrained embedded devices. Therefore, a more effective memory detection solution is needed. Summary of the Invention
[0005] The embodiments of this application aim to provide a more effective memory detection solution to solve the deficiencies in the prior art.
[0006] To achieve the above object, on the one hand, the present application provides a memory detection method, which is executed when a task is running. The task includes the definition of a first variable and the access to a memory space. The memory space is divided into a storage area and a coding area during initialization. Among them, the storage area includes a plurality of storage units of the same size, and the coding area includes a plurality of read-write indication units of the same size respectively corresponding to the plurality of storage units. The method includes: allocating a first storage space for the first variable in the storage area; allocating a non-readable and non-writable alignment red zone immediately following the first storage space in the storage area, where the alignment red zone belongs to the first storage unit and is used to align the first storage space to the size of at least one storage unit; setting a first code in the read-write indication unit corresponding to the first storage unit in the coding area, and modifying the values stored in the last predetermined number of bytes in the alignment red zone to the number of readable and writable bytes in the first storage unit, so as to perform memory detection before accessing the memory.
[0007] In this memory detection method, by setting the last predetermined number of bytes in the alignment red zone to represent the number of bytes of readable and writable data in the storage unit, it is possible to indicate the read-write ability of 4*N bytes through at least 2-bit data, thus solving the problem of excessive memory space occupied by the coding area in the prior art.
[0008] In an implementation manner, the memory space is divided into a storage area and a coding area in a predetermined ratio during initialization, and the predetermined ratio is determined in advance based on the size of the storage unit and the size of the read-write indication unit. In this implementation manner, by dividing the storage area and the coding area in the memory in a predetermined ratio, when the memory pool is issued by other devices, there will be no memory conflict and memory detection error caused by determining the coding area through a predetermined offset as in the prior art.
[0009] In an implementation manner, the size of the storage unit is 4*N bytes, where N is an integer greater than or equal to 1, and the value of N is determined in advance based on the size of the memory. In this implementation manner, by setting the size of the red zone to 4*N bytes and setting N according to the size of the device memory, for devices with smaller memory, N can be set to a smaller value, and for devices with larger memory, N can be set to a larger value, and the minimum is 4 bytes, which is more suitable for embedded devices.
[0010] In one embodiment, the first storage space is located in the stack area or the global area. Among them, setting a first encoding in the read-write indication unit corresponding to the first storage unit in the encoding area, and modifying the value stored in the last predetermined number of bytes in the aligned red zone to the number of readable and writable bytes in the first storage unit includes, by calling the red zone setting function through the red zone setting function interface inserted at compile time, setting a first encoding in the read-write indication unit corresponding to the first storage unit in the encoding area, and modifying the value stored in the last predetermined number of bytes in the aligned red zone to the number of readable and writable bytes in the first storage unit.
[0011] Since resource-constrained hardware limits the size of the burned code, if too much content is instrumented during compiler compilation, the size of the compiled file will be too large, making it impossible to successfully burn the image into the hardware. In this embodiment, by only inserting the red zone setting function interface during compilation instead of the complete red zone setting code, when setting the encoding area encoding for each variable, the function interface can be inserted, and only the code of this function needs to be recorded once in all the code, thus greatly reducing the size of the burned code.
[0012] In one embodiment, the first storage space is located in the heap area, and the first storage space belongs to the first node in the heap area. The method further includes, before allocating the first storage space for the first variable in the storage area, allocating a first red zone for the first variable in the storage area and storing the node header information of the first node in the first red zone, where the size of the first red zone is the size of one storage unit. In this embodiment, storing the node header information in the red zone in the heap area further reduces the memory occupancy.
[0013] In one embodiment, the size of the read-write indication unit is greater than or equal to 2 bits. In this embodiment, by setting the minimum space of the read-write indication unit to 2 bits, the occupied space of the encoding area can be reduced.
[0014] In one embodiment, the method further includes, before accessing the memory, determining the first read-write indication unit of the encoding area corresponding to the access address; reading the encoding corresponding to the storage unit where the access address is located from the first read-write indication unit; and determining whether to perform the memory access based on the read encoding.
[0015] In one implementation, the encoding corresponding to the storage unit where the access address is located is the first encoding. Determining whether to perform memory access based on the read encoding includes reading a first value stored in the last predetermined number of bytes in the storage unit where the access address is located, and determining whether the access address is readable and writable based on the first value to determine whether to perform memory access.
[0016] On the other hand, this specification provides a computer device or computer system, including a memory and a processor. An executable code is stored in the memory. When the processor executes the executable code, the above-mentioned method in any one of the items is implemented. The computer system or device is, for example, an embedded device
[0017] On the other hand, this specification provides a computer program product, including executable code. When the executable code is executed on one or more processors, the one or more processors are caused to implement the above-mentioned method in any one of the items.
[0018] On the other hand, this specification provides a computer storage medium. The computer storage medium is non-volatile. The computer storage medium includes executable code. When the executable code is executed on one or more processors, the one or more processors are caused to implement the above-mentioned method in any one of the items.
[0019] The implementation manners and beneficial effects of the above aspects can be referred to each other. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] By describing the embodiments of the present application in conjunction with the drawings, the embodiments of the present application can be made clearer:
[0021] Figure 1 A schematic diagram of a scenario showing the implementation of a memory detection scheme according to an embodiment of the present application;
[0022] Figure 2 A schematic diagram showing the memory detection scheme according to an embodiment of the present application;
[0023] Figure 3 A flowchart showing a memory detection method for a memory stack area according to an embodiment of the present application;
[0024] Figure 4 A schematic diagram showing a memory pool divided into a storage area and an encoding area in a certain proportion;
[0025] Figure 5 A schematic diagram showing the data structure of a global linked list established in the case where there are multiple memory pools in the device;
[0026] Figure 6 A schematic diagram showing a part of stack area 1 corresponding to task 1 allocated based on the code on the right side in Table 1;
[0027] Figure 7 A flowchart showing the execution of a memory detection function;
[0028] Figure 8 A flowchart showing a memory detection method for a memory heap area according to another embodiment of the present application;
[0029] Figure 9 A schematic diagram showing two heap nodes allocated to variables p1 and p2;
[0030] Figure 10 A schematic diagram showing the heap area 1 allocated to task 1;
[0031] Figure 11 A schematic diagram showing the execution of memory detection function 2 for one byte in the release address;
[0032] Figure 12 A schematic diagram showing the heap area 1 and the corresponding encoding unit after the execution of the instruction free(p1); Detailed implementation manners
[0033] Next, the technical solutions in the embodiments of the present application will be described in conjunction with the accompanying drawings.
[0034] Figure 1 A schematic diagram of a scenario showing the implementation of the memory detection solution according to the embodiments of the present application. As Figure 1As shown in the figure, in this scenario, it includes a computer 11, an IOT device 12, and a terminal device 13. Among them, the IOT device 12 includes an embedded device 121. For example, the IOT device 12 is a smart camera. In addition to components such as a lens, an image sensor, a microphone, and a power supply system that a traditional camera has, the smart camera also includes an embedded device 121 connected to the above components. The embedded device 121 is a system used to control the IOT device 12, and it is a system that combines software and hardware. The hardware of the embedded device 121 is, for example, a printed circuit board, which includes a processor (CPU), memory, a storage device, a WiFi module, interfaces, etc. Among them, since the embedded device 121 is usually small in size and low in cost, its overall resources are limited. For example, its memory is usually made of random access memory (RAM). RAM is expensive, and the software system in the embedded device performs relatively simple functions and has a not very high demand for RAM space. Therefore, the memory of the embedded device usually uses a relatively small space, such as dozens of megabytes. In order to provide a software system for the embedded device 121, the compiler running in the computer 11 compiles the source program of the operating system of the embedded device 121 to obtain an executable file of the operating system. Then, the computer 11 connects to the embedded device 121 and burns the executable file into the embedded device 121. After the embedded device 121 is started, the processor executes the executable file of the operating system burned into it, so that tasks can be performed based on the operating system. For example, the video captured by the camera is stored in the storage device, etc., and it can connect to a local area network or the Internet through the WiFi module to communicate with other terminal devices 13. For example, the captured video is sent to the terminal device 13, etc. During the process of the processor in the embedded device 121 performing various tasks, the memory will be continuously accessed. The embodiment of the present application proposes a real-time memory detection scheme that can be used for the embedded device 121 with limited memory, which requires less additional resources for the memory while ensuring a good detection rate.
[0035] Figure 2 shows a schematic diagram of the memory detection scheme according to an embodiment of the present application. As Figure 2As shown in the figure, on the left side, first, in computer 11, the operating system source program (OS source code) is compiled by a compiler. During the compilation process, the OS source code is converted into machine code by the compiler, and at the same time, the machine code is instrumented. The instrumentation refers to inserting predetermined code at predetermined positions to achieve a predetermined function. For example, the instrumentation may include inserting code for defining a red zone before the code for defining a variable, inserting a red zone setting function interface after the inserted code for defining a red zone, inserting a memory detection function interface before a memory access instruction, and so on. After the compilation of the OS source code by the compiler is completed, an OS executable file is obtained. The computer 11 is connected to the embedded device 121, and the OS executable file is burned into the storage device of the embedded device 121 through a predetermined software, thereby completing the preparation of the embedded device 121. The storage device is, for example, a read-only memory (ROM).
[0036] After being powered on, the prepared embedded device 121 is started by the processor executing the OS executable file therein. After the device is started, the processor first initializes the memory based on the operating system. During this initialization process, the processor divides the RAM space into a storage area and an encoding area. The encoding area is used to store the encoding corresponding to the unit space in the storage area, and this encoding reflects the read / write ability of the corresponding unit space. In this application, through a novel encoding scheme, the number of bits of each encoding can be as few as 2 bits, thus saving the memory space occupied by the encoding area. After the initialization process, the processor runs other codes in the OS executable file, and this code usually includes multiple functions, and each function can be regarded as a task to be executed. Specifically, the processor can allocate a process (or thread, etc.) to each task to run the corresponding function code. When allocating a process, the processor will allocate a certain amount of memory space to this process. This memory space includes, for example, a stack area (Stack) for storing function local variables, a heap area (Heap) dynamically allocated for storing variables, a global area for storing global variables, and so on. When the process runs the code of the function corresponding to the task, it will allocate space for storing variables in each area of the memory according to the instructions in the code, and allocate memory space used as a red zone between the variables. The red zone is an unreadable and unwritable area in the memory. By separating the variables in the memory with the red zone, it is convenient to detect the memory and prevent memory access errors. After allocating the red zone in the memory, an encoding corresponding to this red zone can be set in the encoding area divided in the memory for memory detection. Before executing the memory access instruction in the function, by calling the memory detection function, the read / write ability of the access address is detected based on the encoding corresponding to the memory access address, so as to determine whether there is a memory access error. If there is an access error, an error alarm is given in real time. If there is no access error, the function continues to be executed. Before the function execution ends, the process clears the encoding corresponding to the task in the encoding area and then returns to the function.
[0037] It can be understood that the above reference Figure 1 and Figure 2 descriptions are only illustrative and not restrictive. For example, although the embodiments of this application are described above as being applicable to the embedded device 121, however, the solution of this application is not limited to the embedded device 121, and can also be applicable to various computers, intelligent terminal devices, etc. The memory detection scheme according to this application will be described in detail below.
[0038] Figure 3 FIG. shows a flowchart of a memory detection method for a memory stack area according to an embodiment of this application. Corresponding to Figure 2 this, this flowchart includes a compilation stage I on the left and a running stage II on the right.
[0039] The compilation stage I is, for example, executed based on a compiler in a computer 11 independent of the IOT device 12 in Figure 1 . As is known to those skilled in the art, during compilation, the compiler allocates stack space for local variables in a function. Therefore, for the stack area, red zones can be inserted into the stack area, and function interfaces and memory detection function interfaces for the red zones can be set through compiler instrumentation. Therefore, the compilation stage I includes the following steps S311 - S314 executed by the compiler.
[0040] First, in step S311, red zones are inserted between variables.
[0041] Table 1 shows the pseudo - code before and after instrumenting the instrument_me function in the OS source code by the compiler.
[0042]
[0043] Table 1
[0044] As shown in the pre - instrumentation pseudo - code on the left side of Table 1, the variables defined in the instrument_me function include: the character array c[6] and the integer array i[2]. Among them, c[6] includes 6 characters and occupies 6 bytes, and i[2] includes two 32 - bit numbers, so it occupies 8 bytes. The stack area in memory is preset by the compiler based on the variables in the program when compiling the OS source code, and then when the OS program runs, the processor allocates the stack area in memory based on the preset of the compiler. For example, as shown in the pre - instrumentation pseudo - code on the left side of Table 1, where char c[6] = {0} is to set 6 bytes of memory for c[6] and initialize each byte to 0. Therefore, for the memory allocation mechanism of the stack area, in order to isolate red zones between stack - area variables, it is necessary to perform instrumentation when compiling the OS source code to set the stack space used as the red zone. As shown in the post - instrumentation pseudo - code on the right side of Table 1, in order to insert red zones between stack - area variables, 3 lines of code corresponding to red zone 1, red zone 2, and red zone 3 are inserted: char rz1[4] = {0}, char rz2[6] = {0}, char rz3[4] = {0}. From the positions and contents of the above 3 lines of code, it can be obtained that the inserted red zone 1 is before the variable c[6] and includes 4 bytes, the inserted red zone 2 is between the variables c[6] and i[2] and includes 6 bytes, and the inserted red zone 3 is after the variable i[2] and includes 4 bytes.
[0045] In this embodiment, 4 bytes are used as a storage unit in the stack area to correspond to one encoding in the encoding area, so that the size of the isolation red zone for separating variables is set to 4 bytes. For example, both red zone 1 and red zone 3 are isolation red zones, and their sizes are both 4 bytes. If the size of a variable is not an integer multiple of 4 bytes, it is necessary to pad the variable size to an integer multiple of 4 bytes by inserting an alignment red zone. For example, the size of variable c[6] is 6 bytes, so it is necessary to insert a 2-byte alignment red zone after variable c[6] to pad the stack space occupied by variable c[6] to 8 bytes. After inserting a 2-byte alignment red zone after variable c[6], it is necessary to insert a 4-byte isolation red zone to separate variable c[6] and i[2]. Thus, red zone 2 between variable c[6] and variable i[2] includes a 2-byte alignment red zone and a 4-byte isolation red zone, totaling 6 bytes.
[0046] It can be understood that although only 4 bytes are used as the size of a storage unit as an example here for description, the embodiments of the present application are not limited thereto. For example, the size of the storage unit can be set to 4*N bytes, where N is a predetermined integer greater than or equal to 1. For example, the size of the storage unit can be 8 bytes, 12 bytes, etc. In an actual scenario, the value of N can be determined according to the memory size of the embedded device. If the embedded device is applied to small devices such as cameras, the memory is usually small, so N can be set to 1 or 2. If the embedded device is applied to large devices such as cars and airplanes, the memory is usually large, so N can be set to a larger value, such as 8 or larger.
[0047] In step S312, an interface for the red zone setting function is inserted.
[0048] After inserting the respective codes for inserting the red zones between variables by the compiler, a call to the red zone setting function, i.e., the red zone setting function interface, can be inserted immediately after this code. This is used to call the red zone setting function after the system starts and after allocating each red zone, so as to set the corresponding encoding for each red zone in memory respectively, and set the storage value of the last byte of the aligned red zone. As shown in the instrumented pseudocode on the right side of Table 1, after each code for inserting a red zone, it includes a call to the red zone setting function: _stack_rz_setting(rz1,4), _stack_rz_setting(rz2,6), _stack_rz_setting(rz3,4). Taking _stack_rz_setting(rz1,4) as an example, the input parameters of the red zone setting function include rz1 and 4, where rz1 is the starting address of the variable rz1, and 4 is the number of bytes included in the variable rz1. That is to say, the two input parameters of the red zone setting function are respectively the starting address and size of the corresponding red zone. This red zone setting function is predefined in the OS source code. After calling this function and executing it, it will set the corresponding encoding, storage value, etc. in memory based on this input parameter. The specific process of this setting will be described in detail in the description of the running phase II below. It can be seen from the right-side code in Table 1 that during the compilation process, after each insertion of a red zone, it is necessary to insert the red zone setting function interface to call the red zone setting function. If instead of calling the function, the red zone setting code is directly inserted, the same red zone setting code will be inserted repeatedly. Therefore, by the form of calling the function, the size of the burned file after compilation is greatly reduced.
[0049] In step S313, insert the memory detection function interface before the read / write instruction.
[0050] First, refer to the pre-instrumented pseudocode on the left side of Table 1. The code *p = 'a' means writing the character 'a' to the address of the pointer p, and the code char*p = c+6 before this code means that the address of the pointer p is the address (i.e., the starting address) of the variable c[6] plus 6. The code *p = 'a' is a write instruction for writing to memory. Therefore, referring to the instrumented pseudocode on the right side of Table 1, insert the memory detection function interface _write_chk(p) before the code *p = 'a'. The input parameter of this function interface is p, that is, the address of the pointer p, which is c+6. In the subsequent running phase, when the memory detection function is executed, based on the address of the pointer p, obtain the encoding corresponding to this address to determine whether the address of p is readable and writable, so as to prevent memory access errors.
[0051] In step S314, insert the encoding cleaning function interface before the end of the code.
[0052] Refer to the instrumented pseudo-code on the right side of Table 1. Before the code "return 0", the code "_stack_sd_clear(bp; sp)" is inserted. This code is used to call the encoding cleaning function to clear the encoding corresponding to the stack area before the function "instrument_me" exits. From the inserted code, it can be seen that when calling the encoding cleaning function, two parameters, bp and sp, need to be input, which are the bottom pointer and the top pointer of the stack area of the stack respectively. The encoding cleaning function finds the encoding corresponding to this stack area based on these two parameters and clears the corresponding encoding.
[0053] After the compiler performs instrumentation during compilation and completes compilation through the above steps, an executable file for the operating system is generated. Then, the computer 11 can be connected to the embedded device 121, and the executable file can be burned into the embedded device 121 through a predetermined burning software, thereby completing the preparation of the embedded device 121.
[0054] After the embedded device 121 is prepared, it starts after being powered on. Then, the processor in the embedded device 121 executes the following steps S321 - S324 in the running stage II based on the executable file of the operating system burned into the embedded device 121.
[0055] First, in step S321, after the operating system of the embedded device 121 starts, during the memory initialization process, an encoding area is set in the memory as the memory space for storing encodings.
[0056] Generally, an embedded device has its own memory space. In some cases, after the embedded device is embedded into the main device, the main device will also allocate a memory space to the embedded device. For example, when the embedded device is embedded into a mobile phone, in this case, after the embedded device is connected to the mobile phone, in addition to having its own memory space, it also has a memory space sent from the mobile phone. That is to say, the embedded device can have multiple mutually separated memory spaces. In the following text, the memory space is referred to as a memory pool.
[0057] In the existing solutions for setting up an encoding area in memory, the starting address of the memory is offset by a preset value to obtain the starting address of the encoding area in the memory. This existing technical solution is usually applied to the Android or Linux platforms. However, in the case of an embedded device where there is a memory pool allocated from another device, the starting address of the allocated memory pool cannot be determined in advance, and thus it is difficult to determine a suitable preset value as the offset to set up the encoding area in the memory. If the address of the encoding area is determined by a preset offset value, there may be a conflict with other memory pools. Moreover, the values stored in the encoding area are difficult to be controlled by the system, and there is a high risk of being modified by other tasks, which will directly affect the accuracy of memory detection.
[0058] To solve the above problems, in the embodiments of the present application, when initializing each memory pool, the processor divides the memory pool into a storage area and an encoding area according to a certain ratio based on the size of each memory pool. Among them, the storage area is an area including a red zone and variables, and it is divided into multiple storage units in units of a predetermined size. The encoding area is divided into multiple encoding units (read-write indication units) corresponding to the multiple storage units respectively. Each encoding unit is used to store an encoding corresponding to the corresponding storage unit, and the encoding indicates whether the corresponding storage unit is readable and writable. In the prior art, the encoding area is also called Shadow Memory. Through the encoding scheme of the embodiments of the present application, only 4 encodings (00, 01, 10, 11) can be used to indicate various read-write situations of each storage unit. Thus, in the encoding area, one encoding can be stored with two bits (bit). For example, through the encoding 00, it indicates that all storage units are readable and writable; through the encoding 01, it indicates that some storage units are readable and writable; through the encoding 10, it indicates that the storage unit is a released space; through the encoding 11, it indicates that all storage units are not readable and not writable. It can be understood that the corresponding relationship between the above encodings and the read-write situations of the storage units is merely illustrative and not restrictive. For example, in another embodiment, it can also be that through the encoding 00, it indicates that all storage units are not readable and not writable, and through the encoding 11, it indicates that all storage units are readable and writable, etc., and no limitation is made thereto. In the following text, the former encoding method will be taken as an example for description.
[0059] For example, assume that the size of a storage unit is 4*N bytes, and the size of a memory pool in an embedded device is M bytes. Assume that the storage area includes x bytes, and the number of storage units included in the storage area is This number is the number of encodings that need to be stored in the encoding area, and the size of each encoding is 2 bits. Thus, The memory space occupied by encodings is bytes, that is, equals 16 * N, so that the memory space occupied by the coding area can be easily obtained bytes.
[0060] Therefore, when the processor initializes each memory pool, it allocates the space at the tail of each memory pool, which accounts for of the memory size, as the coding area, and sets the coding area as being used to prevent other tasks from using this area. Figure 4 Figure shows a schematic diagram of dividing a memory pool into a storage area and a coding area in a certain proportion. As Figure 4 shown, the memory pool is divided into a storage area 41 and a coding area 42. The memory space of the storage area 41 can be allocated for each task to use. For example, the storage area 41 includes a stack area and a heap area allocated to a specific task. In addition, in the storage area 41, for example, 4 bytes are used as one storage unit, and in the coding area, 2 bits are used as one coding unit. Each storage unit in the storage area 41 corresponds to one coding unit in the coding area. As Figure 4 shown, the size of each square in the storage area 41 is 1 byte, each storage unit includes 4 bytes, the size of each large rectangular grid in the coding area 42 is 1 byte, and the size of each dotted square in the rectangular large grid is 2 bits. For example, the heap area is the starting address in the storage area. Therefore, the storage unit 0 in the heap area corresponds to the coding unit of the lowest 2 bits of the first byte in the coding area, the storage unit 1 corresponds to the coding unit of the second lowest 2 bits of the first byte in the coding area, and so on. Similarly, the storage unit 4 in the stack area corresponds to the coding unit of the lowest 2 bits of the second byte in the coding area.
[0061] In the case where an embedded device includes multiple memory pools, the processor uses a doubly linked list in, for example, a cache (Cache) to record the starting addresses of each memory pool and the starting addresses of the coding areas in each memory pool for memory allocation, coding setting, and memory detection during memory access. Figure 5 Figure shows the data structure of the global linked list established in the case where there are multiple memory pools in the device. As Figure 5 shown, for memory pool 1 (Pool1), the starting address of this memory pool (PoolStartAddr) and the starting address of the coding area in this memory pool (SDSartAddr) are recorded in the data structure, and are linked to the corresponding data of memory pool 2 (Pool2) through pointers.
[0062] It can be understood that although in the above text, an example of storing one code with 2 bits in the coding area is described, however, the embodiments of the present application are not limited thereto. For example, in a case where more than 2 bits of space are required to store more than 4 codes, the proportion of the coding area in the memory pool can be calculated based on the same calculation process as in the above text, so as to determine the starting address of the coding area in the memory pool based on this proportion.
[0063] In step S322, after starting to execute task 1, a red area is allocated to task 1 in the storage area and a code corresponding to the red area is set in the coding area.
[0064] Assume that task 1 corresponds to the instrumented pseudo-code on the right side of Table 1 above. After starting to execute task 1, the CPU starts to execute the instrumented pseudo-code line by line. First, for the instruction char rz1[4] = {0}, this instruction is equivalent to allocating memory space for the local variable rz1[4] of task 1. In a C language program, for example, stack space is usually allocated for local variables within a function. Specifically, the CPU first determines the memory pool (such as memory pool 1) used to allocate memory for task 1 through the Figure 5 linked list shown, and then records the stack top pointer (SP) and stack bottom pointer (BP) in its register. For example, a certain starting address in memory pool 1 can be determined for this task 1 as the address of the stack bottom pointer (BP) of stack area 1. Then, starting from BP, 4 bytes of red area 1 are allocated based on the code char rz1[4] = {0}, so that SP increases by 4 from BP. As described above, red area 1 is an isolated red area and includes 4 bytes.
[0065] After executing the instruction char rz1[4] = {0}, the instruction _stack_rz_setting(rz1, 4) is executed. As described above, this instruction is used to call the red area setting function for performing memory detection-related settings for the red area. Among them, rz1 and 4 are parameters passed in by the compiler. rz1 is the relative starting address of red area 1 relative to the stack bottom, and 4 is the length of red area 1. For example, red area 1 is arranged at the bottom of stack area 1, so rz1 = 0. In the case where the targeted red area is an isolated red area, all storage units where this red area 1 is located are non-readable and non-writable. Therefore, the above settings include finding the address of the coding unit in the coding area corresponding to red area 1 in memory pool 1 and writing the code "11", that is, all non-readable and non-writable, at this address.
[0066] In this application, corresponding to the above encoding method, in the function for setting in the red area, the address of the coding unit is determined based on the following formula and the coding is written. Specifically, assuming that MemAddr is the starting address of the storage unit where the target red area is located, and PoolStarAddr is the starting address of the memory pool where the target red area is located, the number n1 of the storage unit to which the target red area belongs is calculated through formula (1), where n1 is an integer greater than or equal to 0:
[0067] (MemAddr - PoolStartAddr) / 4*N = n1 (1),
[0068] As described above, since each storage unit corresponds to 2 bits in the coding area, therefore, the n1th storage unit corresponds to 2*n1 bits in the coding area. Dividing this number of bits by 8 gives the byte number n2 in the coding area corresponding to the n1th storage unit, as represented by formula (2):
[0069] (2*n1) / 8 = n1 / 4 = n2 (2),
[0070] That is, the n1th storage unit corresponds to the n2th byte in the coding area, where n2 is an integer greater than or equal to 0. By taking the remainder of dividing 2*n1 bits by 8, the unit number n3 in the n2th byte corresponding to the target red area is obtained, as represented by formula (3):
[0071] n1 % 4 = n3 (3),
[0072] That is, the target red area corresponds to the n3th unit in the n2th byte in the coding area, where n3 is 0, 1, 2, or 3. Thus, based on the following formula (4) and formula (5), the coding unit corresponding to this red area can be set to the coding "SDSetValue" corresponding to this target red area:
[0073] * (SDSartAddr + n2) &= ((~0x03) << (n3*2)) (4)
[0074] * (SDSartAddr + n2) |= (SDSetValue << (n3*2)) (5).
[0075] Among them, as described above, SDSartAddr is the starting address of the encoding area in the memory pool where the red area is located. Among them, formula (4) represents clearing the encoding unit corresponding to the target red area, and formula (5) represents assigning a corresponding encoding (i.e., SDSetValue) to the encoding unit corresponding to the target red area. Thus, by calling the red area setting function, the corresponding encoding can be set in the encoding unit corresponding to the red area. It can be understood that here the encoding unit corresponding to the red area can also be directly assigned through formula (5). For example, when initializing the encoding area, each bit of the encoding area is pre-assigned as zero, and after the subsequent exit function, the encoding unit that has been set relative to this function is cleared, so that it is not necessary to clear through formula (4).
[0076] For example, for red area 1, through Figure 5 the linked list shown, the starting address PoolStartAddr of memory pool 1 and the starting address SDSartAddr of the encoding area in memory pool 1 can be obtained. Assume that the starting address of memory pool 1 is 0, the starting address of the encoding area SDSartAddr is 100, the starting address (i.e., the stack bottom address) of stack area 1 allocated to task 1 is 16, and N = 1. By calling _stack_rz_setting(rz1, 4), the CPU can determine that the starting address of red area 1 is 16 based on the starting address of stack area 1 and rz1, and based on the length 4 of red area 1 passed in, it can determine that the starting address MemAddr of the storage unit corresponding to red area 1 is the starting address of red area 1, i.e., 16, and it can be determined that all storage units corresponding to red area 1 are non-readable and non-writable, i.e., the corresponding encoding (i.e., SDSetValue) is "11". Thus, according to formula (1), n1 = 4 can be calculated, n2 = 1 can be obtained according to formula (2), and n3 = 0 can be obtained according to formula (3), that is, the 0th unit of the 1st byte in the encoding area. According to formula (4), the two bits of the 0th unit of the 1st byte in the encoding area are cleared, and through formula (5), the two bits of the 0th unit of the 1st byte in the encoding area are assigned the value "11".
[0077] Figure 6 Schematically shows a part of stack area 1 corresponding to task 1 allocated based on the right-side code in Table 1. In Figure 6 it, the shaded squares represent the red area, and the non-shaded squares represent the variable storage space. As Figure 6 shown, starting from the starting address (e.g., address 16) pointed to by the stack bottom pointer, first, 4 bytes of red area 1 are allocated based on the code char rz1[4] = {0}, and the memory space allocated to red area 1 is storage unit 4 in the storage area. After allocating red area 1, as Figure 6As shown by the arrow below storage unit 4, by executing the instruction _stack_rz_setting(rz1, 4), the 0th encoding unit (i.e., the lowest 2 bits of the byte) in the byte corresponding to the address corresponding to Red Zone 1 in the encoding area (i.e., SDSartAddr + 1 = 100 + 1 = 101) is assigned "11".
[0078] After executing the instruction _stack_rz_setting(rz1, 4), continue to execute downward. Execute the instruction char c[6] = {0}, as Figure 6 shown, that is, allocate 6 bytes to variable c after Red Zone 1. Then, execute the following instruction char rz2[6] = {0}. Similar to the execution of char rz1[4] = {0} above, allocate 6 bytes after variable c as Red Zone 2, as Figure 6 shown. The starting address of Red Zone 2 is rz2. Among them, the first 2 bytes in Red Zone 2 are the alignment red zone, which is located in storage unit 6, and the last 4 bytes in Red Zone 2 are the isolation red zone, which is located in storage unit 7. After allocating the space for Red Zone 2, execute the red zone setting function corresponding to Red Zone 2, that is, _stack_rz_setting(rz2, 6). The CPU sets the corresponding encodings for storage unit 6 and storage unit 7 corresponding to these 6 bytes respectively by calling _stack_rz_setting(rz2, 6). Specifically, based on the passed-in parameter rz2 and the stack bottom pointer, here, as Figure 6 shown, rz2 should be 10, so the starting address of Red Zone 2 can be obtained as 10 + 16 = 26. Based on the length 6 of Red Zone 2, the starting address of storage unit 6 can be obtained as the starting address of Red Zone 2 minus 2, that is, 26 - 2 = 24, and the starting address of storage unit 7 is the starting address of Red Zone 2 plus 2, that is, 26 + 2 = 28.
[0079] After obtaining the starting addresses of storage unit 6 and storage unit 7 respectively, the encoding units corresponding to storage unit 6 and storage unit 7 can be determined according to the above formulas (1)-(3). That is, first determine n1 according to formula (1), and then determine n2 and n3 based on n1 according to formulas (2) and (3). Then assign values to the corresponding encoding units according to formulas (4) and (5). For example, for storage unit 6, its starting address is 24. According to formulas (1)-(3), n1 = (24 - 0) / 4 = 6, n2 = n1 / 4 = 1, n3 = n1 % 4 = 2, that is, the encoding unit corresponding to storage unit 6 is the 2nd unit of the 1st byte in the encoding area, as Figure 6 shown by the arrow below storage unit 6. Similarly, it can be determined that storage unit 7 corresponds to Figure 6The encoding unit 3 in it. For the storage unit 6, after the CPU divides the red area 2 into the storage unit 6 and the storage unit 7, it can be determined that the storage unit 6 includes the last 2 red area bytes at the tail, so it is partially readable and writable. That is, the encoding corresponding to the storage unit 6 is "01", and it can be determined that the number of readable and writable bytes in the storage unit 6 is 2 bytes. At the same time, it can be determined that all the bytes in the storage unit 7 are red area bytes, so the storage unit 7 is completely unreadable and unwritable. Therefore, the encoding corresponding to the storage unit 7 is "11".
[0080] Thus, after determining that the storage unit 6 corresponds to the encoding unit 2, by executing formulas (4) and (5), the encoding unit 2 is assigned the value "01". At the same time, the value stored in the last byte of the storage unit 6 is modified to the number of readable and writable bytes in the storage unit 6, that is, 0x02, where 0x indicates that 02 is a hexadecimal number. By indicating the partially readable and writable storage units in this way, since the number of readable and writable bytes in this partially readable and writable storage unit is recorded in the last byte of the storage unit, only one encoding is needed to indicate various partially readable and writable situations in the storage unit, instead of using multiple encodings to respectively indicate multiple partially readable and writable situations as in the prior art, where each partially readable and writable situation has a different number of readable and writable bytes. Thus, the total number of encodings required can be greatly reduced, that is, the number of bits of each encoding is reduced, thereby saving the space occupied by the encoding area. Here, because the number of readable and writable bytes is recorded in the last byte of the storage unit, and the maximum value that a byte can represent is 255, the size 4*N of the storage unit should be less than or equal to 256. It can be understood that in this application, it is not limited to recording the number of readable and writable bytes through the last byte of the partially readable and writable storage unit. For example, the number of readable and writable bytes can be recorded through the last two bytes. In this case, the size of the storage unit can be set larger.
[0081] Similarly to the above, after determining that the storage unit 7 corresponds to Figure 6 the encoding unit 3 in it, the encoding unit 3 can be assigned the value "11" by executing formulas (4) and (5). In addition, by pre-setting the values of each encoding unit to 0 when initializing the memory, there is no need to separately set the value of the corresponding encoding unit for the storage unit 5.
[0082] After executing the above code _stack_rz_setting(rz2,6), the subsequent code int i[2] = {0}, char rz3[4] = {0}, _stack_rz_setting(rz3,4) can be executed similarly to the above, which will not be elaborated here.
[0083] Step S323, perform memory detection before accessing the memory.
[0084] Instructions corresponding to memory access include memory read instructions and memory write instructions. Before executing these two types of instructions, memory detection needs to be performed to determine whether the access address is readable and writable. For example, referring to the pseudocode on the right side of Table 1, after executing the instructions corresponding to allocating the red zone, variables, and setting the red zone encoding, execute the instruction char*p = c + 6, that is, define the address of pointer p as the starting address of c plus 6, and execute the instruction _write_chk(p) before executing the memory write instruction *p = 'a', that is, call the memory detection function to detect whether the address of p is readable and writable, where the input parameter p of this function is the address of pointer p.
[0085] Figure 7 The flowchart showing the execution of the memory detection function is as follows Figure 7 As shown, after inputting the memory access address into the memory detection function, first, in step S71, determine the address of the encoding unit corresponding to the access address, and read the encoding corresponding to the storage unit where the access address is located.
[0086] In the embodiment of the present application, corresponding to the above encoding method, the encoding corresponding to the memory access address AccAddr is read in the memory detection function based on the following formula. Specifically, calculate the number n1 of the storage unit to which the access address belongs through formula (6), where n1 is an integer greater than or equal to 0:
[0087] (AccAddr - PoolStartAddr) / 4*N = n1 (6),
[0088] Then, calculate the byte number n2 in the encoding area corresponding to the storage unit of the access address and the encoding unit number n3 in the n2th byte corresponding to the access address respectively through the following formulas (7) and (8):
[0089] (2*n1) / 8 = n1 / 4 = n2 (7)
[0090] n1 % 4 = n3 (8)
[0091] The calculation process of formulas (6)-(8) is the same as that of the above formulas (1)-(3), and will not be elaborated here.
[0092] After obtaining n2 and n3 corresponding to the access address, the encoding SDSetValue corresponding to the access address can be obtained through formula (9):
[0093] SDSetValue = ( * (SDSartAddr + n2) >> (n3*2)) & (0x03) (9).
[0094] Formula (9) indicates that after shifting the value of the byte corresponding to the access address in the coding area (i.e., SDSartAddr + n2) to the right by (n3 * 2) bits and performing an AND operation with 00000011, the result of the calculation is the coding SDSetValue corresponding to the access address AccAddr.
[0095] Specifically, when executing the above instruction _write_chk(p), the address of p is c + 6. Refer to Figure 6 , the address of p is the address corresponding to rz2 in the figure, which is 26. From the above calculation for address 26 through formulas (1)-(3), for address p, it can be obtained that n2 = 1 and n3 = 2, that is, address p corresponds to Figure 6 the coding unit 2 in address 101 in. Thus, the coding corresponding to address p can be read as "01" through formula (9).
[0096] In step S72, it is determined whether the read coding is "00". If the read coding is "00", it indicates that the access address is readable and writable, so the detection process can be ended and the function can be returned. If the read coding is not "00", then step S73 is performed to determine whether the read coding is "11". If the read coding is "11", it indicates that the access address is not readable and not writable, so the detection process is ended and a BOF error is reported. If the read coding is not "11", then step S74 is performed to determine whether the read coding is "10". If it is, the detection process is ended and a UAF error is reported. If not, the read coding is "01", so step S75 is performed to read the stored value N of the last byte in the storage unit where the access address is located. Specifically, based on the number n1 of the storage unit corresponding to the access address calculated through formula (6), the address of the storage unit can be calculated, and thus the value N of the last byte in the storage unit can be read.
[0097] Then step S76 is performed to determine whether the byte order n4 of the access address in its storage unit is less than N. If it is, the detection process is ended and the function is returned. If not, the detection process is ended and a BOF error is reported. Specifically, the byte order n4 of the access address in its storage unit can be calculated through the following formula (10):
[0098] (AccAddr - PoolStartAddr) % (4 * N) + 1 = n4 (10)
[0099] For example, refer to Figure 6, the access address p corresponds to the address 26 (i.e., rz2) in storage unit 6, so n4 = (26 - 0) % 4 + 1 = 2 + 1 = 3, and the N stored in the last byte of storage unit 6 is 2. Since 3 is less than 2, that is, the address p is not readable or writable, so a BOF error is reported.
[0100] It can be understood that the detection logic in the above detection function is merely illustrative rather than restrictive. For example, in the detection function, it is possible to first determine which one of the four encodings the read encoding is, and then execute the corresponding detection logic.
[0101] Step S324, clear the encoding corresponding to stack area 1 before the function returns.
[0102] Continue to refer to the pseudocode on the right side of Table 1. In the case where the above memory detection passes, continue to execute the instruction. Before the function returns (return), the instruction _stack_sd_clear(bp, sp) is also executed. This instruction is used to call the stack area encoding cleaning function, where bp is the stack bottom pointer and sp is the stack top pointer. When executing the stack area encoding cleaning function, two encoding units in the encoding areas corresponding to bp and sp are found based on the above formulas (6)-(8), and the two encoding units and the encoding units between them are all cleared to zero, so as to clear the encoding corresponding to stack area 1. After clearing these encodings, the function returns, and the processor automatically clears stack area 1 in the memory, so that the memory space previously allocated to stack area 1 becomes available for other tasks, and the encoding units corresponding to stack area 1 are also cleared to zero for encoding the memory space when the memory space of stack area 1 is used again.
[0103] The memory detection method is described above by taking the stack area as an example. There may also be other data structures similar to the stack area in the device memory, such as the global area. Therefore, the above method can also be used to detect the memory of the global area.
[0104] Figure 8 Shows a flowchart of a memory detection method for a memory heap area according to another embodiment of the present application. This flowchart includes a source code modification stage I, a compilation stage II, and a running stage III.
[0105] Since the allocation in the heap area is performed at runtime based on the programmer's instruction malloc(), rather than based on the instructions compiled by the compiler as in the stack area, in order to also separate variables by isolating the red zone and align the variable lengths by aligning the red zone as in the stack area, it is necessary to modify the underlying code of the malloc() function before compilation. At the same time, a call to the red zone setting function can be inserted into the malloc() function, and the code of the red zone setting function can be added to the OS source code to set the corresponding encoding in the memory encoding area after allocating heap memory during device operation.
[0106] Modification phase I can be performed by computer 11 in Figure 1 before compilation phase II. Generally, when the original malloc() function is executed, it performs the following operations: establishing a node header, storing information related to the heap node in the node header, such as the address of the previous node, for forming a node linked list, etc. This node header is not used to store variables and is not readable or writable; allocating the heap space for the variables applied through the malloc() function after the node header. That is to say, every time the malloc() function is called in the code, a node is formed in the heap area at runtime, and this node includes the node header and the variable space. The heap space applied through the malloc() function will not be automatically released by the compiler and is usually released by the programmer by calling the function free() in the program for the heap space of the applied variables. For example, the following code may be included in the OS source code:
[0107] char*p1=(char*)malloc(6)
[0108] char*p2=(char*)malloc(4)
[0109] …
[0110] free(p1)
[0111] free(p2)
[0112] …
[0113] During the running phase, when executing the above code, two heap nodes will be allocated for variables p1 and p2 respectively. Figure 9 The schematic diagram shows the schematic diagram of the two heap nodes allocated for variables p1 and p2. As Figure 9As shown, node 1 is a heap node allocated to variable p1. This node 1 includes a 2-byte node header (shown as shaded squares) and a variable storage space for 6 nodes (shown as white squares). Node 2 is a heap node allocated to variable p2. This node 2 includes a 2-byte node header and a 4-byte variable storage space. When free(p1) and free(p2) are executed subsequently, the spaces of node 1 and node 2 are released and can be allocated again.
[0114] From Figure 9 It can be seen that in the heap area, there are already unreadable and unwritable node headers separating the variables. However, both the node headers and the variable storage spaces are not integer multiples of the storage unit size. Therefore, it is not convenient to encode as shown above. For this reason, in source code modification stage I, the underlying function of malloc() is modified to align the size of the node header to the size of the storage unit (e.g., 4 bytes) to be used as a red zone, and the variable space is aligned to at least the size of one storage unit by inserting aligned red zones. After making the above modifications to the malloc() function, a red zone setting function interface is also added to the malloc() function. Similarly to the stack area, by calling the red zone setting function, the encoding corresponding to the red zone is set in the encoding area, and the value of the last byte is set in the aligned red zone. In this source code modification stage, the underlying code of the free() function is also modified. First, a memory detection function 2 is inserted after the start of the free() function. This memory detection function 2 is used to detect whether there is a double-free problem. Then, a red zone setting function interface is also added to the free() function to set the encoding in the encoding area corresponding to the node space to "10" after releasing the node space.
[0115] In compilation stage II, compared to the stack area, since the steps of allocating the red zone and setting the red zone encoding during the execution stage are achieved by modifying the OS source code, in compilation stage II, only a memory detection function interface needs to be inserted before the memory access instruction and an encoding cleanup function interface needs to be inserted before the function return instruction.
[0116] In runtime stage III, for example, when performing the above task 1, in the case where task 1 includes the above few lines of code for variables p1 and p2, the CPU executes the following steps S81 - S84.
[0117] In step S81, a heap node corresponding to the variable is allocated, and the encoding corresponding to the red zone is set in the encoding area.
[0118] For example, when executing the instruction char* p1 = (char*)malloc(6), based on the modified malloc() function as described above, node 1' is allocated at the starting address of the free memory (e.g., address 200). This node 1' includes a 4-byte node header, a 6-byte space for variable p1, and a 2-byte alignment red zone after variable p1. Figure 10 Schematic diagram showing the heap area 1 allocated to task 1. Node 1' allocated to variable p1 is as Figure 10 shown.
[0119] After node 1' is allocated in memory, the red zone setting function will be called through the red zone setting function interface inserted in the modified malloc() function. The call of this red zone setting function can refer to the description of the stack area above. By inputting the starting address and length of the red zone as parameters to this red zone setting function, corresponding encodings can be set in the encoding units corresponding to the red zone in the encoding area through the above formulas (1)-(5). As Figure 10 shown, by calling this red zone setting function, "11" is written in encoding unit 0 corresponding to the red zone where the node header of node 1' is located in the encoding area, indicating that the storage unit corresponding to this red zone is not readable and not writable. "01" is written in encoding unit 2 corresponding to the alignment red zone of node 1' in the encoding area, indicating that the storage unit where this alignment red zone is located is partially readable and writable. At the same time, similar to the red zone setting function for the stack area above, by calling the red zone setting function, the last byte in the alignment red zone is also modified to the number of readable and writable bytes in its storage unit, that is, "0x02". Similarly, "11" is written in encoding unit 3 corresponding to the node header of node 2'.
[0120] After executing the instruction char* p1 = (char*)malloc(6), execute char* p2 = (char*)malloc(4). Similar to the above, node 2' corresponding to variable p2 is allocated in memory. As Figure 10 shown, in node 2', a 4-byte node header and a 4-byte memory space for variable p2 are allocated, and at the same time, the encoding corresponding to the node header in node 2' is set in the encoding area.
[0121] In step S82, after starting to execute free(p1), memory detection function 2 is executed.
[0122] After starting to execute free(p1), through the interface of memory detection function 2 inserted in free(p1) in the source code modification, memory detection function 2 is called to perform memory detection on the released address. Figure 11Schematic diagram showing the execution of the memory detection function 2 for one byte in the release address. First, in step S111, calculate the encoding unit address corresponding to the release address and read the encoding from this encoding unit address. This step can refer to the description of step S71 in the above text and will not be elaborated here. In step S112, determine whether the read encoding is "10". If it is, report an error: there is a multiple release error. If not, continue to execute the free(p1) function. When calling the memory detection function 2 in free(p1), the process shown in Figure 11 is executed once for each byte in the variable p1, thus completing the memory detection.
[0123] In step S83, release the heap node space and modify the encoding corresponding to the heap node variable.
[0124] When continuing to execute free(p1), based on the modified free() function, first release the space of node 1' as free space, and then store "10" in the encoding unit corresponding to the storage space of the variable p1 in node 1', indicating that this space is the space after releasing the heap memory. It can only be accessed after applying for this space through the malloc function and setting the corresponding encoding unit to "00", thereby preventing the occurrence of UAF errors in memory access in this way. Figure 12 Schematic diagram showing heap area 1 and the corresponding encoding units after executing the instruction free(p1). As Figure 12 shown, after executing free(p1), the space corresponding to node 1' is released, and the two encodings in the encoding area corresponding to the storage unit where the variable p1 is located are modified to "10".
[0125] In step S84, clear the encoding corresponding to heap area 1 before the function returns.
[0126] This step can refer to the description of step S324 in the above text and will not be elaborated here.
[0127] An embodiment of the present application also provides a computer device, including a memory and a processor. An executable code is stored in the memory. When the processor executes the executable code, the above method is implemented. The computer device is, for example, an embedded device.
[0128] Among them, the memory can be used to store software programs and modules, which mainly includes a program storage area and a data storage area. The program storage area can store the operating system and application programs required to implement the above-mentioned memory detection method. The data storage area can store configuration files of applications, etc. In addition, the memory can be a volatile memory, such as a random-access memory (RAM); the memory can also be a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid-state drive (SSD); the memory can also include a combination of the above types of memories.
[0129] The processor is the control center of the device, connecting various parts of the device through various interfaces and lines. By running or executing software programs and / or modules stored in the memory, and calling data stored in the memory, it executes the memory detection method. Optionally, the processor can include one or more processing units.
[0130] Through the above embodiments, the memory detection solution according to the present application has been described in detail. In the memory detection solution of the present application, a 2-bit encoding is used to represent the memory state of 4*N bytes, and a new detection algorithm is combined to detect existing memory problems, greatly reducing the memory space occupied by the encoding area; according to the hardware memory resource situation, the size of the inserted red zone is preset; by automatically allocating the storage area and the encoding area at a certain ratio during memory pool initialization, memory detection errors can be avoided, and based on this allocation method, when setting the encoding corresponding to the red zone, the encoding unit corresponding to the storage unit where the red zone is located can be found based on this allocation ratio; in addition, by inserting function interfaces such as the red zone memory setting function interface during compilation, rather than directly inserting the code corresponding to the relevant functions into the source code, the size of the compiled file can be effectively reduced.
[0131] It should be understood that the descriptions such as "first" and "second" in this article are only used to distinguish similar concepts for the simplicity of description and do not have other limiting effects.
[0132] Those of ordinary skill in the art should also be further aware that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be implemented by electronic hardware, computer software, or a combination of the two. To clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described according to functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present invention.
[0133] As described above, the above is only a preferred specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by those skilled in the art within the technical scope disclosed by the present invention should be covered by the protection scope of the present invention. Therefore, the protection scope of the present invention should be subject to the protection scope of the claims.
Claims
1. A memory detection method, which is executed when a task is running. The task includes the definition of a first variable and the access to a memory space. Characterized in that, The memory space is divided into a storage area and a coding area during initialization. Among them, the storage area includes a plurality of storage units of the same size, and the coding area includes a plurality of read-write indication units of the same size corresponding to the plurality of storage units respectively. The method includes: Allocating a first storage space for the first variable in the storage area; Allocating a non-readable and non-writable alignment red zone immediately following the first storage space in the storage area. The alignment red zone belongs to the first storage unit among the plurality of storage units and is used to align the first storage space to the size of at least one storage unit; Setting a first code in the read-write indication unit corresponding to the first storage unit in the coding area, and modifying the value stored in the last predetermined number of bytes in the alignment red zone to the number of readable and writable bytes in the first storage unit, for memory detection before accessing the memory.
2. The method according to claim 1, Characterized in that, The memory space is divided into a storage area and a coding area at a predetermined ratio during initialization. The predetermined ratio is determined in advance based on the size of the storage unit and the size of the read-write indication unit.
3. The method according to claim 1, Characterized in that, The size of the storage unit is 4*N bytes, where N is an integer greater than or equal to 1, and the value of N is determined in advance based on the size of the memory.
4. The method according to claim 1, Characterized in that, The first storage space is located in the stack area or the global area. Among them, setting a first code in the read-write indication unit corresponding to the first storage unit in the coding area and modifying the value stored in the last predetermined number of bytes in the alignment red zone to the number of readable and writable bytes in the first storage unit includes, by calling a red zone setting function through a red zone setting function interface inserted during compilation, setting a first code in the read-write indication unit corresponding to the first storage unit in the coding area, and modifying the value stored in the last predetermined number of bytes in the alignment red zone to the number of readable and writable bytes in the first storage unit.
5. The method according to claim 1, the first storage space is located in the heap area, and the first storage space belongs to the first node in the heap area. Characterized in that, The method further includes, before allocating a first storage space for the first variable in the storage area, allocating a first red zone for the first variable in the storage area and storing the node header information of the first node in the first red zone. The size of the first red zone is the size of one storage unit.
6. The method according to claim 1, Characterized in that, The size of the read-write indication unit is greater than or equal to 2 bits.
7. The method according to any one of claims 1-6, Characterized in that, It further includes, before accessing the memory, determining a first read-write indication unit of the encoding area corresponding to the access address; reading the encoding corresponding to the storage unit where the access address is located from the first read-write indication unit; Based on the read encoding, determining whether to perform the access to the memory.
8. The method according to claim 7, wherein, the encoding corresponding to the storage unit where the access address is located is the first encoding, and based on the read encoding, determining whether to perform the access to the memory includes reading a first value stored in the last predetermined number of bytes in the storage unit where the access address is located, and determining whether the access address is readable and writable based on the first value to determine whether to perform the access to the memory.
9. A computer system, including a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the method according to any one of claims 1-8 is implemented.
Citation Information
Patent Citations
Method and device for automatically positioning internal memory fault
CN103197999A
Application memory error detection method, detection device and storage medium
CN109150642A