Realization method of memory border crossing detection and memory border crossing detection method

By generating and automatically inserting memory out-of-bounds detection functions at compile time, the problem of low memory out-of-bounds detection in the prior art is solved, and more efficient and accurate memory out-of-bounds detection is achieved without relying on runtime libraries.

CN120029886APending Publication Date: 2025-05-23SHANDONG YUNHAI GUOCHUANG CLOUD COMPUTING EQUIP IND INNOVATION CENT CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510239608.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-28
Publication Date
2025-05-23

AI Technical Summary

Technical Problem

In the prior art, the accuracy of memory out-of-bounds detection is low, and some memory operation blocks are easily missed, resulting in inaccurate detection results.

Method used

Automatic memory out-of-bounds detection functions are generated based on structure pointers and out-of-bounds detection logic at compile time, and these functions are inserted in each memory access operation.

Benefits of technology

Improves the accuracy of memory out-of-bounds detection, avoids omissions caused by manual instrumentation, and does not rely on runtime libraries, avoiding the troubles caused by library version and compatibility issues.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120029886A_ABST
    Figure CN120029886A_ABST
Patent Text Reader

Abstract

The invention discloses a memory border crossing detection implementation method and a memory border crossing detection method, which are applied to the technical field of computers, and comprise the following steps: generating a memory border crossing detection function based on a structural body pointer and border crossing detection logic; the structural body pointers comprise all structural bodies of parameters required by memory cross-border detection; the border-crossing detection logic comprises border-crossing judgment logic corresponding to each parameter in the structural body pointer; and during compiling, identifying memory access operations in the program, and inserting a memory border-crossing detection function in each memory access operation to obtain a target program, so as to detect a memory border-crossing behavior in the real-time operating system based on the target program. The memory crossing detection function is automatically inserted according to the memory operation, a user does not need to manually write an instrumentation code, the insertion efficiency and the instrumentation accuracy are improved, some memory operation blocks are prevented from being omitted, the memory crossing detection accuracy is improved, detection can be directly carried out based on the memory crossing detection function, and the detection efficiency is improved. And dependence on a runtime library is not needed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method for implementing memory cross-boundary detection and a memory cross-boundary detection method. Background Art

[0002] In embedded real-time operating systems, programs use memory very frequently, requiring frequent memory application, release, and reading and writing. High-frequency use will inevitably increase the probability of memory out-of-bounds, causing a series of problems that are difficult to reproduce and solve. Therefore, it is necessary to perform memory out-of-bounds detection in a timely manner to ensure that potential problems can be discovered in time when memory is used. However, the current memory out-of-bounds detection method requires R&D personnel to manually insert detection code at the memory operation block, which makes it easy to miss some memory operation blocks, resulting in low accuracy of memory out-of-bounds detection.

[0003] Therefore, how to improve the accuracy of memory cross-boundary detection in a real-time operating system is a technical problem that those skilled in the art urgently need to solve. Summary of the invention

[0004] In view of this, an object of the present invention is to provide a method for implementing memory cross-boundary detection and a method for memory cross-boundary detection, which solves the problem of low accuracy of memory cross-boundary detection in the prior art.

[0005] In order to solve the above technical problems, the present invention provides a method for implementing memory cross-bounds detection, comprising:

[0006] Generate a memory out-of-bounds detection function based on a structure pointer and an out-of-bounds detection logic; wherein the structure pointer includes a structure of all parameters required for memory out-of-bounds detection; and the out-of-bounds detection logic includes an out-of-bounds judgment logic corresponding to each parameter in the structure pointer;

[0007] During compilation, memory access operations in the program are identified, and the memory cross-border detection function is inserted into each of the memory access operations to obtain a target program, so as to detect memory cross-border behaviors in a real-time operating system based on the target program.

[0008] On the one hand, the structure pointer includes array out-of-bounds access, pointer arithmetic error, string operation error and structure array out-of-bounds access; wherein, the array out-of-bounds access means that when accessing the array, the accessed index exceeds the set valid range; the pointer arithmetic error means that when performing pointer operation, the calculated address points to an illegal memory area.

[0009] On the one hand, the memory out-of-bounds detection function is generated based on the structure pointer and the out-of-bounds detection logic, including:

[0010] Determine that the structure pointer is an out-of-bounds access to the array, and the corresponding out-of-bounds detection logic is whether there is an array out-of-bounds access;

[0011] Determine that the structure pointer is the pointer arithmetic error, and the corresponding out-of-bounds detection logic is whether there is a pointer arithmetic error;

[0012] Determine that the structure pointer is the string operation error, and the corresponding out-of-bounds detection logic is whether there is a string buffer overflow;

[0013] Determine that the structure pointer is out of bounds for the structure array, and the corresponding out-of-bounds detection logic is whether there is a structure array out of bounds;

[0014] The memory out-of-bounds detection function is generated based on each of the structure pointers and the corresponding out-of-bounds detection logic.

[0015] On the one hand, the above-mentioned memory out-of-bounds detection implementation method also includes:

[0016] Generate a memory out-of-bounds error reporting function based on an error reporting parameter structure; wherein the error reporting parameter structure is a structure for determining an error type and an error location;

[0017] Accordingly, during compilation, memory access operations in the program are identified, and the memory out-of-bounds detection function is inserted into each of the memory access operations to obtain a target program, including:

[0018] During compiling, the memory access operations in the program are identified, and the memory cross-boundary detection function and the memory cross-boundary error reporting function are inserted into each of the memory access operations to obtain the target program.

[0019] On the one hand, during compilation, the memory access operation in the program is identified, and the memory out-of-bounds detection function and the memory out-of-bounds error reporting function are inserted into each of the memory access operations. After obtaining the target program, the method further includes:

[0020] The real-time operating system including the target program is run to obtain a memory cross-boundary detection result and a memory cross-boundary error report.

[0021] On the one hand, when the error report parameter structure is a structure including an error type parameter, an array index parameter where the error occurred, an array size parameter, a storage call stack address parameter, a call stack size parameter, a file name parameter where the error occurred, and a line number parameter where the error occurred;

[0022] The step of running the real-time operating system including the target program to obtain a memory cross-bounds detection result and a memory cross-bounds error report includes:

[0023] Running the real-time operating system including the target program to obtain the memory cross-border detection result and the memory cross-border error report including the process number, error type, accessed array index, array size, error occurrence location and call stack information;

[0024] The process number is used to locate the running instance where the error occurs;

[0025] The error type is used to determine the type of memory error;

[0026] The accessed array index is used to determine the accessed array subscript;

[0027] The array size is used to determine whether an out-of-bounds access occurs;

[0028] The error occurrence location is used to determine the file name, line number and code location corresponding to the code where the memory out-of-bounds problem occurs;

[0029] The call stack information is used to determine the call path of the target program when an error occurs.

[0030] On the one hand, during compiling, memory access operations in the program are identified, and the memory out-of-bounds detection function is inserted into each of the memory access operations to obtain a target program, so as to detect memory out-of-bounds behavior in the real-time operating system based on the target program, including:

[0031] Determine the risk level of each memory out-of-bounds type.

[0032] Determine the cross-border detection timing corresponding to each cross-border risk level setting; wherein the higher the cross-border risk level, the greater the corresponding cross-border detection frequency;

[0033] The memory out-of-bounds detection function, the out-of-bounds risk level and the out-of-bounds detection timing are inserted into each of the memory access operations to obtain the target program.

[0034] The present invention also provides a memory cross-border detection method, comprising:

[0035] Receive events from running real-time operating systems;

[0036] Starting the real-time operating system, and detecting the memory cross-border behavior in the real-time operating system based on the target program to obtain a memory cross-border detection result;

[0037] The target program is a program obtained based on the above-mentioned method for implementing memory out-of-bounds detection.

[0038] The present invention also provides a device for implementing memory cross-bounds detection, comprising:

[0039] A memory out-of-bounds detection function generation module, used to generate a memory out-of-bounds detection function based on a structure pointer and an out-of-bounds detection logic; wherein the structure pointer includes a structure of all parameters required for memory out-of-bounds detection; the out-of-bounds detection logic includes an out-of-bounds judgment logic corresponding to each parameter in the structure pointer;

[0040] The automatic plug-in module is used to identify memory access operations in a program during compilation, and insert the memory out-of-bounds detection function into each memory access operation to obtain a target program, so as to detect memory out-of-bounds behavior in a real-time operating system based on the target program.

[0041] The present invention also provides a memory cross-border detection device, comprising:

[0042] An event receiving module, used for receiving events of running the real-time operating system;

[0043] A memory cross-border detection module is used to start the real-time operating system and detect the memory cross-border behavior in the real-time operating system based on the target program to obtain a memory cross-border detection result;

[0044] The target program is a program obtained based on the above-mentioned method for implementing memory out-of-bounds detection.

[0045] The present invention also provides an electronic device, comprising:

[0046] Memory for storing computer programs;

[0047] A processor is used to execute the computer program to implement the above-mentioned memory cross-border detection implementation method and the steps of the above-mentioned memory cross-border detection method.

[0048] The present invention also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for implementing memory out-of-bounds detection as described above and the steps of the memory out-of-bounds detection method as described above are implemented.

[0049] The purpose of the embodiments of the present invention is to provide a method for implementing memory cross-boundary detection and a method for memory cross-boundary detection, which can solve the technical problem of low accuracy of memory cross-boundary detection.

[0050] To solve the above technical problems, an embodiment of the present invention provides a method for implementing memory out-of-bounds detection, which may include: generating a memory out-of-bounds detection function based on a structure pointer and out-of-bounds detection logic; wherein the structure pointer includes a structure of all parameters required for memory out-of-bounds detection; the out-of-bounds detection logic includes an out-of-bounds judgment logic corresponding to each parameter in the structure pointer; during compilation, identifying memory access operations in the program, and inserting a memory out-of-bounds detection function into each memory access operation to obtain a target program, so as to detect memory out-of-bounds behavior in a real-time operating system based on the target program.

[0051] It can be seen from the above technical scheme that the beneficial effect of the present invention is that compared with the current manual instrumentation that omits certain memory operation blocks, resulting in low accuracy of memory out-of-bounds detection, the present invention automatically inserts the designed memory out-of-bounds detection function according to the memory operation during the compilation process, thereby eliminating the need for R&D personnel to manually write instrumentation code, improving the efficiency of insertion and the accuracy of instrumentation, preventing the omission of certain memory operation blocks, thereby improving the accuracy of memory out-of-bounds detection, and can directly perform detection based on the memory out-of-bounds detection function without relying on the runtime library, avoiding the troubles caused by library version and compatibility issues.

[0052] In addition, the present invention also provides a device, equipment and computer-readable storage medium for implementing memory cross-border detection, as well as a memory cross-border detection device, equipment and computer-readable storage medium, which also have the above-mentioned beneficial effects. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] In order to more clearly illustrate the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0054] Figure 1 A flowchart of a method for implementing memory cross-bounds detection provided by an embodiment of the present invention;

[0055] Figure 2 A schematic diagram of a structure pointer provided by an embodiment of the present invention;

[0056] Figure 3 A schematic diagram of a memory cross-bounds detection function processing flow provided by an embodiment of the present invention;

[0057] Figure 4 A schematic diagram of an error report parameter structure provided by an embodiment of the present invention;

[0058] Figure 5 A schematic diagram of a memory out-of-bounds error reporting function provided by an embodiment of the present invention;

[0059] Figure 6 An example flowchart of a memory cross-border detection method provided by an embodiment of the present invention;

[0060] Figure 7 An example flowchart of a memory cross-border detection method provided by an embodiment of the present invention;

[0061] Figure 8 A schematic diagram of the structure of a device for implementing memory cross-bounds detection provided by an embodiment of the present invention;

[0062] Fig. 9 A schematic diagram of the structure of a memory cross-border detection device provided by an embodiment of the present invention;

[0063] Fig.10 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0064] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0065] The terms "including" and "having" in the specification of the present invention and the above-mentioned drawings, as well as any variations thereof, are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device including a series of steps or units is not limited to the listed steps or units, but may include steps or units that are not listed.

[0066] Some of the terms that appear in the description of the embodiments of the present invention may be interpreted as follows:

[0067] CPU: It is the abbreviation of "Central Processing Unit", which is the core component of computer hardware, responsible for executing instructions in the program and processing data.

[0068] MMU: The abbreviation of "Memory Management Unit" is the control circuit used to manage virtual memory and physical memory in the central processing unit (CPU).

[0069] Malloc (function for dynamic memory allocation): malloc is a function in the C language standard library for dynamic memory allocation. It allows you to apply for a memory area of ​​a specified size when the program is running and returns a pointer to the memory area. If the memory allocation is successful, malloc returns a non-NULL pointer; if it fails, it returns NULL.

[0070] Free: free is a function in the C language standard library that is used to release memory that was previously dynamically allocated using malloc, calloc, or realloc functions. After the memory is released, the memory becomes available again and can be used by subsequent memory allocation requests.

[0071] ASan: AddressSanitizer is a powerful memory error detection tool that detects memory errors by instrumenting at compile time and monitoring memory access at runtime. ASan can detect a variety of memory problems, including memory out of bounds, use of freed memory, stack overflow, etc.

[0072] operator new / delete: In C++, operator new and operator delete are global operators used for dynamic memory allocation and deallocation. They are used to allocate memory and deallocate previously allocated memory, respectively.

[0073] RTOS: A real-time operating system is an operating system designed specifically for embedded systems that provides determinism and real-time performance. RTOS can respond to external events within a predetermined time and has functions such as task scheduling, resource management, synchronization and communication.

[0074] Strcpy: The function is used to copy one string into another string. It copies characters from the beginning of the source string until it encounters a null character ('\0').

[0075] Strcat: The function is used to concatenate one string to the end of another string. It finds the end of the target string and then copies characters from the beginning of the source string until it encounters a null character ('\0').

[0076] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0077] Next, a method for implementing memory cross-boundary detection provided by an embodiment of the present invention is described in detail. Figure 1 A flowchart of a method for implementing memory cross-bounds detection provided by an embodiment of the present invention, the method may include:

[0078] S101, generating a memory out-of-bounds detection function based on a structure pointer and an out-of-bounds detection logic; wherein the structure pointer includes a structure of all parameters required for memory out-of-bounds detection; and the out-of-bounds detection logic includes an out-of-bounds judgment logic corresponding to each parameter in the structure pointer.

[0079] The execution subject of this embodiment is an electronic device. This embodiment does not limit specific electronic devices. The electronic device in this embodiment can be a computer, an embedded real-time operating system, etc. The structure pointer in this embodiment is used to pass and operate parameters in a function. All memory out-of-bounds detections in this embodiment refer to all types of current memory out-of-bounds detections, as long as there is a memory out-of-bounds behavior, to improve the comprehensiveness of memory out-of-bounds detection. The out-of-bounds detection logic in this embodiment is used to detect different types of memory out-of-bounds behaviors.

[0080] It should be further explained that, in order to improve the comprehensiveness of the parameters in the structure pointer, the above structure pointer includes array out-of-bounds access, pointer arithmetic error, string operation error and structure array out-of-bounds access; wherein, array out-of-bounds access means that when accessing an array, the accessed index exceeds the set valid range; pointer arithmetic error means that when performing pointer operations, the calculated address points to an illegal memory area. This embodiment encapsulates memory out-of-bounds into a memory out-of-bounds detection function with a structure pointer as a parameter. In this structure, all parameters required for out-of-bounds checking can be defined. An example of a parameter structure is as follows: Figure 2 , Figure 2 A schematic diagram of a structure pointer provided by an embodiment of the present invention, Figure 2The types of structure pointers are shown in the table, where int array_index: an integer variable used to store the index value of an array. intarray_size: an integer variable used to store the size or length of an array. int* pointer: an integer pointer used to point to the memory address of integer data. int* base_pointer: an integer pointer used to point to the base address of an array or memory block. size_t pointer_size: a size_t type variable, usually used to store the size of a memory block. size_t is an unsigned integer suitable for representing memory size. const char* str: a pointer to a constant character, usually used for string operations. size_t buffer_size: a size_t type variable, which may be used to store the size of a buffer. int struct_array_index: an integer variable, which may be used to store the index value of a structure array. int struct_array_size: an integer variable, which may be used to store the size or length of a structure array. These variables are encapsulated in a structure and passed to the memory out-of-bounds detection function so that the function can access and check the relevant memory area. This embodiment provides a specific structure pointer, which includes all memory out-of-bounds parameters, thereby improving the comprehensiveness of the parameters in the structure pointer.

[0081] It should be further explained that, based on any of the above embodiments, the above generation of a memory out-of-bounds detection function based on a structure pointer and out-of-bounds detection logic may include: determining that a structure pointer is an array out-of-bounds access, and the corresponding out-of-bounds detection logic is whether there is an array out-of-bounds access; determining that a structure pointer is a pointer arithmetic error, and the corresponding out-of-bounds detection logic is whether there is a pointer arithmetic error; determining that a structure pointer is a string operation error, and the corresponding out-of-bounds detection logic is whether there is a string buffer overflow; determining that a structure pointer is a structure array out-of-bounds, and the corresponding out-of-bounds detection logic is whether there is a structure array out-of-bounds; generating a memory out-of-bounds detection function based on each structure pointer and its corresponding out-of-bounds detection logic. For ease of understanding, please refer to Figure 3 , Figure 3 A schematic diagram of a memory cross-bounds detection function processing flow provided by an embodiment of the present invention, Figure 3The workflow of a memory out-of-bounds detection function is described. This function receives a structure pointer as a parameter, which contains the information required for memory out-of-bounds checking. The following is a detailed explanation of the flowchart: Memory out-of-bounds detection function entry parameter: structure pointer. The starting point of the flowchart is the entry of the memory out-of-bounds detection function, which receives a pointer to a structure as a parameter. This structure contains all the information required for memory out-of-bounds checking. The function first checks whether there is an array out-of-bounds behavior. If there is an array out-of-bounds (marked as "Y" yes), the array out-of-bounds behavior is obtained, the process enters the termination procedure, and an error report is output. If there is no array out-of-bounds (marked as "N" no), the next step of the check is continued. Next, the function checks whether there is a pointer arithmetic error. If there is a pointer arithmetic error (marked as "Y"), the pointer arithmetic error behavior is obtained, the process enters the termination procedure, and an error report is output. If there is no pointer arithmetic error (marked as "N"), the next step of the check is continued. Then, the function checks whether there is a string buffer overflow behavior. If there is a string buffer overflow (marked as "Y"), the string buffer overflow behavior is obtained, the process enters the termination program, and an error report is output. If there is no string buffer overflow (marked as "N"), the next step of checking is continued. Then check whether there is a structure array out-of-bounds behavior. If there is a structure array out-of-bounds (marked as "Y"), the structure array out-of-bounds behavior is obtained, the process enters the termination program, and an error report is output. If there is no structure array out-of-bounds (marked as "N"), the next step of checking is continued. Finally, the function performs other types of memory out-of-bounds checks. If other memory out-of-bounds behaviors are found (marked as "Y"), other array out-of-bounds behaviors are obtained, the process enters the termination program, and an error report is output. If no other memory out-of-bounds are found (marked as "N"), the process enters a normal exit. This embodiment provides a processing flow of a memory out-of-bounds detection function, which improves the accuracy of the memory out-of-bounds detection function in performing out-of-bounds detection. The memory out-of-bounds detection function in this embodiment does not depend on the runtime library.

[0082] S102, during compilation, identifying memory access operations in the program, and inserting a memory cross-bounds detection function into each memory access operation to obtain a target program, so as to detect memory cross-bounds behavior in the real-time operating system based on the target program.

[0083] This embodiment identifies the memory access operation in the program during compilation, so that a memory out-of-bounds detection function can be inserted into each memory access operation to prevent omission. The real-time operating system in this embodiment can be an embedded real-time operating system. The target program in this embodiment is used to detect memory out-of-bounds behavior in the real-time operating system when the real-time operating system is running. Inserting a memory out-of-bounds detection function in each memory access operation can be understood as a plugging process. This embodiment does not limit the specific plugging method, as long as automatic plugging can be performed based on the plugging method. For example, the memory operation code can be automatically plugged during compilation based on ASan (memory error detection tool), and the detection timing and detection frequency of each memory out-of-bounds detection function can be determined. The implementation principle of ASan includes two parts: compilation and operation. The present invention will not use the running part because the running part is not suitable for real-time operating systems. During compilation, ASan will insert code before each memory read and write statement, and detect whether the current memory access is legal based on the state of the shadow memory corresponding to each memory access. This method does not require the user to manually add monitoring code for each memory operation. It can be understood that when compiling this embodiment, the -fsanitize=address compilation option is added, and all memory operations are stubbed with the help of ASan. The code examples before and after compilation and stub insertion are as follows:

[0084] Before compiling the instrumentation:

[0085] BEGIN

[0086] / / Allocate memory to store 10 integers

[0087] ptr=ALLOCATE_MEMORY(10 * SIZE_OF_INTEGER)

[0088] / / Release previously allocated memory

[0089] FREE_MEMORY(ptr)

[0090] / / Try to access the first element of the freed memory pointer

[0091] value = ptr[0] / / Invalid memory is accessed here, which should result in undefined behavior

[0092] END

[0093] After compiling the instrumentation:

[0094] BEGIN

[0095] / / Allocate memory to store 10 integers

[0096] ptr=ALLOCATE_MEMORY(10 * SIZE_OF_INTEGER)

[0097] / / Check validity of memory allocation

[0098] ALLOC_STATUS=__ASan_report_alloc(ptr,(10*SIZE_OF_INTEGER),0, 0)

[0099] IF ALLOC_STATUS != 0 THEN

[0100] / / Handle errors, for example: return or output error information

[0101] HANDLE_ERROR()

[0102] ENDIF

[0103] / / Release previously allocated memory

[0104] FREE_MEMORY(ptr)

[0105] / / Report memory release

[0106] __ASan_report_free(ptr,(10*SIZE_OF_INTEGER),0)

[0107] / / Check the loading operation of freed memory

[0108] LOAD_STATUS=__ASan_report_load1(ptr)

[0109] IF LOAD_STATUS != 0 THEN

[0110] / / Handle errors, for example: return or output error information

[0111] HANDLE_ERROR()

[0112] ENDIF

[0113] / / Try to access already released memory

[0114] value = ptr[0] / / Accessing invalid memory at this time should result in undefined behavior

[0115] END.

[0116] It should be further explained that, based on any of the above embodiments, during compilation, memory access operations in the program are identified, and a memory out-of-bounds detection function is inserted into each memory access operation to obtain a target program, so as to detect memory out-of-bounds behavior in the real-time operating system based on the target program, which may include: determining the out-of-bounds risk level corresponding to each memory out-of-bounds type; determining the out-of-bounds detection timing corresponding to each out-of-bounds risk level setting; wherein, the higher the out-of-bounds risk level, the greater the corresponding out-of-bounds detection frequency; inserting a memory out-of-bounds detection function, as well as the out-of-bounds risk level and the out-of-bounds detection timing into each memory access operation to obtain the target program. It can be understood that an out-of-bounds risk level can be assigned to each memory out-of-bounds type (such as array out-of-bounds, pointer arithmetic error, etc.). This level is determined based on the possibility and potential impact of the operation causing an error. For example: high risk: directly accessing memory outside the array boundary (such as arr[size]). Medium risk: unallocated memory areas may be accessed through pointer arithmetic. Low risk: accessing a buffer of known size, but the operation complexity is high. According to the out-of-bounds risk level, the corresponding out-of-bounds detection timing and frequency are set for each level. The higher the level, the greater the frequency of detection to ensure that high-risk operations are adequately monitored: High risk: detection is performed on each access. Medium risk: periodic detection, such as each function call or loop iteration. Low risk: detection is performed under specific conditions, such as in critical parts of the program. After the above steps are processed, the compiled target program will automatically perform memory out-of-bounds detection at runtime. This program not only contains the original function code, but also additional out-of-bounds detection logic. In an environment based on a real-time operating system (RTOS), this enhanced program can be used to detect and prevent memory out-of-bounds behavior, balancing performance overhead and detection accuracy by dynamically adjusting the detection frequency, ensuring that high-risk operations are adequately monitored, while avoiding excessive detection of low-risk operations.

[0117] It should be further explained that, based on any of the above embodiments, the implementation method of the above memory out-of-bounds detection may also include: generating a memory out-of-bounds error reporting function based on an error reporting parameter structure; wherein the error reporting parameter structure is a structure for determining the error type and error location; correspondingly, during compilation, identifying the memory access operations in the program, and inserting a memory out-of-bounds detection function into each memory access operation to obtain a target program, including: during compilation, identifying the memory access operations in the program, and inserting a memory out-of-bounds detection function and a memory out-of-bounds error reporting function into each memory access operation to obtain a target program. The error reporting parameter structure in this embodiment is used to store the parameters required for the error report. After performing detection based on the memory out-of-bounds function in this embodiment, a memory out-of-bounds error report may be obtained. The target program in this embodiment may also generate a memory out-of-bounds error report, and the memory out-of-bounds error report helps the user to deeply understand the background and occurrence conditions of the error, so as to repair it more effectively.

[0118] It should be further explained that, based on any of the above embodiments, the memory access operations in the above identification program, and the memory out-of-bounds detection function and the memory out-of-bounds error reporting function are inserted into each memory access operation, and after obtaining the target program, it can also include: running a real-time operating system including the target program to obtain a memory out-of-bounds detection result and a memory out-of-bounds error report. The memory out-of-bounds detection result in this embodiment can be used to determine the existence of a memory out-of-bounds, and the memory out-of-bounds error report in this embodiment is used to record detailed information of the memory out-of-bounds, such as the out-of-bounds type, the out-of-bounds location, etc. This embodiment can not only obtain a memory out-of-bounds detection result, but also a memory out-of-bounds error report, thereby improving the comprehensiveness of subsequent memory out-of-bounds error analysis.

[0119] It should be further explained that, based on the above embodiment, when the error report parameter structure is a structure including an error type parameter, an array index parameter where the error occurred, an array size parameter, a storage call stack address parameter, a call stack size parameter, a file name parameter where the error occurred, and a line number parameter where the error occurred; running a real-time operating system including a target program to obtain a memory out-of-bounds detection result and a memory out-of-bounds error report may include: running a real-time operating system including a target program to obtain a memory out-of-bounds detection result, and a memory out-of-bounds error report including a process number, an error type, an accessed array index, an array size, an error occurrence location, and call stack information; the process number is used to locate the running instance where the error occurred; the error type is used to determine the type of memory error; the accessed array index is used to determine the accessed array subscript; the array size is used to determine whether an out-of-bounds access has occurred; the error occurrence location is used to determine the file name, line number, and code location corresponding to the code where the memory out-of-bounds problem occurred; the call stack information is used to determine the calling path of the target program when the error occurred. For ease of understanding, please refer to Figure 4 , Figure 4A schematic diagram of an error report parameter structure provided by an embodiment of the present invention, wherein const char * error_type: a string pointer pointing to a constant character, used to describe the type of error. For example, it can be "array out of bounds", "pointer arithmetic error", etc. int access_index: an integer variable, which may be used to store the array index value when an out-of-bounds access occurs. int array_size: an integer variable, used to store the size or length of the array, so as to compare with access_index to determine whether an out-of-bounds access occurs. void* backtrace_array

[10] : an array containing 10 void pointers, which may be used to store call stack trace (backtrace) information when an error occurs. Each pointer may point to a stack frame or function information. size_t backtrace_size: a size_t type variable, used to store the number of stack trace information actually stored in backtrace_array. const char * code_file: a string pointer pointing to a constant character, used to store the code file name where the error occurs. int code_line: an integer variable, used to store the code line number where the error occurs. The error report parameter structure can be used to define the parameters of the error report. It encapsulates the detailed information when the error occurs, so as to generate an error report or perform error handling. In the memory out-of-bounds detection function, when an error is detected, this structure can be filled and passed to the error reporting function to generate a detailed error report. The definition of this structure helps to standardize the data format of the error report, making the error handling logic clearer and more consistent. Please refer to Figure 5 , Figure 5A schematic diagram of a memory out-of-bounds error reporting function provided by an embodiment of the present invention, wherein the output error reporting function (memory out-of-bounds error reporting function) entry parameter: the structure pointer function receives a pointer to a structure as a parameter. This structure contains all the necessary information required to generate an error report. The structure contains the following fields, each of which is used to describe different aspects of the error report: the process number is used to record the identification number of the process where the error occurred. This helps to identify the source of the error in a multi-process environment. The error type is used to describe the specific type of the error, such as "array out-of-bounds", "pointer arithmetic error", "string buffer overflow", etc. The accessed array index is used to record the array index value when the out-of-bounds access occurs. This helps to determine which array access caused the error. The array size is used to record the actual size of the array, which is used to compare with the accessed index value to determine whether it is out of bounds. The error location is used to describe the specific location where the error occurred, which may include information such as the file name, line number, and code snippet. The call stack information is used to record the call stack information when the error occurs, which helps developers understand the context of the error. The call stack information usually includes a series of function calls, from the top-level function to the function where the error occurs. The memory out-of-bounds error report includes the following contents: error type, process number, accessed array index and array size, location where the error occurred (file name, line number) and call stack information. This embodiment provides the specific memory of the memory out-of-bounds error report, which improves the comprehensiveness of the content of the memory out-of-bounds error report.

[0120] It should be further explained that in order to improve the accuracy of memory cross-border detection, the above-mentioned memory cross-border detection method may also include: obtaining historical cross-border behavior data, training a machine learning algorithm based on the historical cross-border behavior data, and obtaining a cross-border behavior prediction model. This embodiment can analyze the memory access pattern through a machine learning algorithm, learn the characteristics of normal behavior, and identify abnormal behavior that deviates from the normal pattern. Thereby, potential cross-border risks can be discovered in advance, such as continuous boundary trial behavior or unreasonable memory access frequency, so as to issue an alarm before the actual cross-border occurs. Or this embodiment can also collect context information of memory access instructions in real time when the program is running, including access address, access size, call stack, memory block status, etc. Extract key features such as access frequency, access mode (continuity, randomness), life cycle of memory block, etc., and obtain historical data. Use historical data (normal behavior and known cross-border behavior) to train a machine learning model, such as a decision tree, support vector machine (SVM) or a deep learning model (such as a recurrent neural network RNN). The goal of the model is to learn the pattern of normal memory access behavior and be able to distinguish abnormal behavior. When the program is running, the real-time collected memory access data is input into the trained model. The model will determine whether the current behavior is likely to be out-of-bounds based on the input data, and if so, an alarm will be issued. When abnormal behavior is detected, the system will not only issue an alarm, but also provide detailed context information, such as: call stack information of access instructions, to help developers quickly locate the problem. The allocation and release history of memory blocks, analyze the possible causes of out-of-bounds behavior. Comparative analysis with other normal behaviors helps understand the characteristics of abnormal behavior. Early warning: It can identify potential risks before out-of-bounds behavior actually occurs, reducing the possibility of security vulnerabilities being exploited. Low false alarm rate: Through the precise analysis of the machine learning model, alarms caused by misjudgment are reduced. Performance optimization: Compared with traditional runtime detection, the intelligent prediction module can reduce unnecessary checks and reduce performance overhead. Strong adaptability: It can automatically learn and adapt to the memory access patterns of different programs without manually configuring rules. It should be noted that in the subsequent use process, the prediction results of the out-of-bounds behavior prediction model and the memory out-of-bounds behavior detected by the target program can be comprehensively processed in this embodiment to prevent missing out-of-bounds behavior, thereby improving the comprehensiveness and accuracy of out-of-bounds behavior detection.

[0121] The implementation method of a memory cross-boundary detection provided by the embodiment of this aspect may include: S101, generating a memory cross-boundary detection function based on a structure pointer and a cross-boundary detection logic; wherein the structure pointer includes a structure of all parameters required for memory cross-boundary detection; the cross-boundary detection logic includes a cross-boundary judgment logic corresponding to each parameter in the structure pointer; S102, during compilation, identifying the memory access operation in the program, and inserting the memory cross-boundary detection function in each memory access operation to obtain a target program, so as to detect the memory cross-boundary behavior in the real-time operating system based on the target program. Compared with the current manual insertion of some memory operation blocks, which leads to low accuracy of memory cross-boundary detection, the present invention automatically inserts the designed memory cross-boundary detection function according to the memory operation during the compilation process, so that the R&D personnel do not need to manually write the insertion code, improve the efficiency of insertion and the accuracy of insertion, prevent the omission of some memory operation blocks, thereby improving the accuracy of memory cross-boundary detection, and can directly detect based on the memory cross-boundary detection function, without relying on the runtime library, avoiding the trouble caused by the version and compatibility issues of the library.

[0122] In embedded real-time operating systems, programs use memory very frequently, and need to frequently apply for, release, read and write memory. High-frequency use will inevitably increase the probability of illegal memory operations, causing a series of problems that are difficult to reproduce and solve. Common memory problems (illegal memory operation problems) generally include memory overflow, memory leak, memory out of bounds, etc. Memory overflow means that the memory requested by the program during operation exceeds the memory capacity provided by the system, or exceeds the memory limit allowed for the program itself, resulting in the program being unable to run or the system crashing. In real-time operating systems, such problems can be prevented by using mature CPU memory management units MMU through methods such as address translation, memory protection, and virtual memory management, or by adding memory usage monitoring tools for effective monitoring. Memory leak refers to the phenomenon that during the program running, the allocated memory space is not correctly released after use, resulting in this part of the memory continuing to be occupied and cannot be reused. Over time, memory leaks will gradually consume available memory, eventually causing program crashes or performance degradation. In real-time operating systems, such problems can be monitored through mature methods and tools, such as preventing memory leaks through the second encapsulation of malloc and free, recording the number of times malloc and free are called in different places, and after the program is finished running, judging whether malloc and free are used in pairs. If they are not used in pairs, the unpaired process is output to achieve the purpose of locating memory leaks. Memory out of bounds means that when a program accesses memory, the read and write operations exceed the memory space allocated for it, resulting in data anomalies in other places in the memory, which in turn causes program anomalies or crashes. Even worse, it may cause the system to be invaded, causing security issues such as data leakage. Because memory out of bounds may cause the value of a variable far away to be destroyed, the problem point is usually far away from the code location of the problem. Different operating conditions may show different abnormal phenomena, so compared with memory overflow and memory leak, memory out of bounds problems are more difficult to reproduce and locate.

[0123] The existing memory out-of-bounds detection methods have the following disadvantages: (1) R&D personnel are required to manually insert detection code at the memory operation block, which makes it easy to miss certain memory operation blocks; (2) R&D personnel are required to determine the timing and frequency of algorithm detection. If the detection timing is too frequent, it is easy to affect the system performance. If the detection is insufficient, it is easy to cause missed detection. (3) These detection methods can only detect the cross-bounds situation that slowly exceeds the boundary of the allocated memory block, and cannot detect the cross-bounds situation caused by jumping access. (4) Dependence on the runtime library. In view of the limitations of the above algorithms, the present invention independently designs a memory out-of-bounds detection function that does not rely on the runtime library, and designs a memory out-of-bounds detection implementation method that can automatically insert the memory out-of-bounds detection function.

[0124] In order to make the present invention easier to understand, please refer to Figure 6 , Figure 6 A flowchart of a memory cross-bounds detection method provided by an embodiment of the present invention may specifically include:

[0125] S201, encapsulate a memory out-of-bounds detection function, where the memory out-of-bounds detection function includes a structure pointer of parameters required for out-of-bounds detection.

[0126] The memory out-of-bounds detection function in this embodiment includes the following out-of-bounds scenarios: a) Array out-of-bounds access: refers to when accessing an array, if the subscript exceeds the valid range (less than 0 or greater than or equal to the size of the array), an array out-of-bounds error will be triggered. b) Pointer arithmetic error: refers to when performing pointer operations, the calculated address may point to an illegal memory area. For example, the wrong increase of the pointer causes it to exceed the allocated memory block. c) String operation error: When processing strings, especially when using functions such as strcpy, strcat, etc., if the destination buffer is not large enough, it may cause a buffer overflow and write beyond the allocated memory. d) Structure array out-of-bounds: refers to when processing a structure array, if the access exceeds the boundary of the structure array, an out-of-bounds error will occur. This embodiment defines the input parameter content, input parameter format, detection logic, etc. according to common memory out-of-bounds scenarios. The algorithm needs to be simple in design, high in execution efficiency, and low in resource usage. The memory out-of-bounds detection function implementation can detect various common memory out-of-bounds scenarios. When there is an out-of-bounds problem, the running program is terminated and a related error report is generated.

[0127] S202, encapsulate a memory out-of-bounds error reporting function; wherein the memory out-of-bounds error reporting function is used to output a memory out-of-bounds location and a memory out-of-bounds type.

[0128] This embodiment can define the input parameter content of the memory out-of-bounds error reporting function, the report output content, the printing format, and when a memory out-of-bounds error occurs, print the error type, access details, memory out-of-bounds call stack and other information. The functions of each part are as follows: a) Error type: Indicates what type of memory error has occurred, such as "array out-of-bounds", "pointer arithmetic error", "string buffer overflow", "structure array out-of-bounds", etc. b) Access details: Provides the address of the error memory, the size of the access, and whether it is a read (READ) or write (WRITE) operation. c) Stack trace: Provides a stack trace starting from the location where illegal memory is accessed in the program to help developers locate the code location where the error occurs. d) Source code at the location where the error occurs: Provides the source code file name and line number at the location where the error occurs. e) Program status when the error occurs: Provides the program status when the error occurs, including register status, program counter, etc.

[0129] S203. When determining compilation, based on the instrumentation technology of ASan (memory error detection tool), memory out-of-bounds detection functions and output error reporting functions are inserted into each memory access operation to obtain a target program, so as to detect out-of-bounds behavior during the operation of a real-time operating system based on the target program.

[0130] In this embodiment, the ASan compilation option is added during compilation, and the system will automatically insert memory out-of-bounds detection functions and memory out-of-bounds error reporting functions according to specific memory operations (such as allocation, release, read, write, etc.), ensuring that every memory access will be checked.

[0131] The beneficial effects of the embodiments of the present invention may include: (1) Automatic code instrumentation: During the compilation process, the system automatically inserts detection code according to specific memory operations (such as allocation, release, read, write, etc.). This process is completely transparent and does not require developers to make any modifications to the original code, reducing the risk of introducing errors manually. (2) Independent of the runtime library: The method of the present invention is designed to be independent of any external library or framework, ensuring that the application can run independently in various environments and avoiding the troubles caused by library version and compatibility issues. At the same time, this independence also helps to reduce resource consumption. (3) More refined memory out-of-bounds monitoring: The memory out-of-bounds detection functions of the present invention cover the monitoring of common memory out-of-bounds operations (including heap, stack, and global memory), ensuring that potential problems can be detected in a timely manner during memory usage. (4) Exhaustive error reporting and analysis tools: When a memory error is found, the system will automatically generate a detailed error report, which will include the following information to help developers deeply understand the background and occurrence conditions of the error, so as to repair it more effectively.

[0132] For the present invention to be more easily understood, please specifically refer to Figure 7 , Figure 7 which is a flowchart example of a memory out-of-bounds detection method provided by the embodiments of the present invention, and specifically may include:

[0133] S301. Receive an event of running a real-time operating system.

[0134] The execution subject of this embodiment is a real-time operating system, for example, an embedded real-time operating system.

[0135] S302. Start the real-time operating system and detect the memory out-of-bounds behavior in the real-time operating system based on the target program to obtain a memory out-of-bounds detection result; wherein, the target program is a program obtained based on the above-mentioned implementation method of memory out-of-bounds detection.

[0136] The target program in this embodiment is a function that can instrument the memory out-of-bounds detection function obtained based on the above-mentioned implementation method of memory out-of-bounds detection.

[0137] It should be further explained that, based on the above embodiment, when the target program includes the memory out-of-bounds error reporting function in the implementation method of the above memory out-of-bounds detection, the real-time operating system is started, and the memory out-of-bounds behavior in the real-time operating system is detected based on the target program to obtain the memory out-of-bounds detection result, which may include: detecting the memory out-of-bounds behavior in the real-time operating system based on the target program to obtain the memory out-of-bounds detection result and the memory out-of-bounds error report. This embodiment provides a detailed memory out-of-bounds error report related to the memory out-of-bounds detection, so that the memory out-of-bounds behavior can be analyzed in detail. It is understandable that it may also include: analyzing the cause of the memory out-of-bounds based on the memory out-of-bounds detection result and the memory out-of-bounds error report; determining repair suggestions based on the cause of the memory out-of-bounds. This embodiment provides specific repair suggestions for memory out-of-bounds, so that the memory out-of-bounds problem can be quickly handled.

[0138] The memory out-of-bounds detection method provided by the embodiment of the present invention may include: S301, receiving an event of running a real-time operating system. S302, starting the real-time operating system, and detecting the memory out-of-bounds behavior in the real-time operating system based on the target program, and obtaining a memory out-of-bounds detection result; wherein the target program is a program obtained based on the above-mentioned memory out-of-bounds detection implementation method. Compared with the current memory out-of-bounds detection that needs to rely on the runtime library and manual plugging leads to missed memory out-of-bounds behaviors, the present invention automatically inserts the designed memory out-of-bounds detection function according to the memory operation, thereby eliminating the need for R&D personnel to manually write the plugging code, improving the efficiency of insertion and the accuracy of plugging, and preventing the omission of certain memory operation blocks, thereby improving the accuracy of memory out-of-bounds detection, and can directly detect based on the memory out-of-bounds detection function without relying on the runtime library, avoiding the trouble caused by the version and compatibility issues of the library.

[0139] The following is an introduction to an implementation device for memory cross-border detection provided by an embodiment of the present invention. The implementation device for memory cross-border detection described below and the implementation method for memory cross-border detection described above can be referred to each other.

[0140] Figure 8 A schematic diagram of a structure of a device for implementing memory cross-bounds detection provided by an embodiment of the present invention may include:

[0141] A memory out-of-bounds detection function generation module 100 is used to generate a memory out-of-bounds detection function based on a structure pointer and an out-of-bounds detection logic; wherein the structure pointer includes a structure of all parameters required for memory out-of-bounds detection; and the out-of-bounds detection logic includes an out-of-bounds judgment logic corresponding to each parameter in the structure pointer;

[0142] The automatic plug-in module 200 is used to identify memory access operations in a program during compilation, and insert the memory out-of-bounds detection function into each of the memory access operations to obtain a target program, so as to detect memory out-of-bounds behavior in a real-time operating system based on the target program.

[0143] Further, based on any of the above embodiments, the above structure pointer may include array out-of-bounds access, pointer arithmetic error, string operation error and structure array out-of-bounds access; wherein, the array out-of-bounds access means that when accessing the array, the accessed subscript exceeds the set valid range; the pointer arithmetic error means that when performing pointer operations, the calculated address points to an illegal memory area.

[0144] Further, based on any of the above embodiments, the memory out-of-bounds detection function generation module 100 may include:

[0145] A first detection logic determination unit is used to determine whether the structure pointer is an out-of-bounds access to the array, and the corresponding out-of-bounds detection logic is whether there is an array out-of-bounds access;

[0146] A second detection logic determination unit is used to determine whether the structure pointer is the pointer arithmetic error, and the corresponding cross-bounds detection logic is whether there is a pointer arithmetic error;

[0147] A third detection logic determination unit is used to determine whether the structure pointer is the string operation error, and the corresponding cross-boundary detection logic is whether there is a string buffer overflow;

[0148] A fourth detection logic determination unit is used to determine whether the structure pointer is out of bounds for the structure array, and the corresponding out-of-bounds detection logic is whether there is an out-of-bounds structure array;

[0149] The fifth detection logic determination unit is used to generate the memory out-of-bounds detection function based on each of the structure pointers and the corresponding out-of-bounds detection logic.

[0150] Further, based on any of the above embodiments, the above memory out-of-bounds detection implementation device may further include:

[0151] A memory out-of-bounds error reporting function generating module generates a memory out-of-bounds error reporting function based on an error reporting parameter structure; wherein the error reporting parameter structure is a structure for determining an error type and an error location;

[0152] Accordingly, the automatic plugging module 200 may include:

[0153] A target program generation module, configured to identify the memory access operations in a program during compilation, and insert the memory out-of-bounds detection function and the memory out-of-bounds error reporting function into each of the memory access operations to obtain the target program.

[0154] Further, based on any of the above embodiments, the method for implementing memory out-of-bounds detection may further include:

[0155] A memory out-of-bounds detection report determination module, configured to run the real-time operating system including the target program to obtain a memory out-of-bounds detection result and a memory out-of-bounds error report.

[0156] Further, based on any of the above embodiments, when the error report parameter structure is a structure including an error type parameter, an array index parameter where the error occurs, an array size parameter, a stored call stack address parameter, a call stack size parameter, a file name parameter where the error occurs, and a line number parameter where the error occurs;

[0157] The memory out-of-bounds detection report determination module may include:

[0158] A memory out-of-bounds detection report determination unit, configured to run the real-time operating system including the target program to obtain the memory out-of-bounds detection result, and the memory out-of-bounds error report including a process ID, an error type, an accessed array index, an array size, an error occurrence location, and call stack information;

[0159] The process ID is used to locate the running instance where the error occurs;

[0160] The error type is used to determine the type of memory error;

[0161] The accessed array index is used to determine the array subscript accessed;

[0162] The array size is used to determine whether an out-of-bounds access has occurred;

[0163] The error occurrence location is used to determine the file name, line number, and code location corresponding to the code where the memory out-of-bounds problem occurs;

[0164] The call stack information is used to determine the call path of the target program when the error occurs.

[0165] Further, based on any of the above embodiments, the automatic instrumentation module 200 may include:

[0166] An out-of-bounds risk level determination unit, configured to determine the out-of-bounds risk level corresponding to each type of memory out-of-bounds;

[0167] A cross-border detection timing determination unit, used to determine the cross-border detection timing corresponding to each cross-border risk level setting; wherein, the higher the cross-border risk level, the greater the corresponding cross-border detection frequency;

[0168] A target program unit is determined based on the detection timing, and is used to insert the memory cross-boundary detection function, the cross-boundary risk level and the cross-boundary detection timing into each memory access operation to obtain the target program.

[0169] It should be noted that the order of the modules and units in the above-mentioned memory cross-boundary detection implementation device can be changed without affecting the logic.

[0170] Figure 8 The description of the features in the corresponding embodiments can be found in Figure 8 The relevant descriptions of the corresponding embodiments will not be repeated here one by one.

[0171] An implementation device for memory cross-boundary detection provided by an embodiment of the present invention may include: a memory cross-boundary detection function generation module 100, which is used to generate a memory cross-boundary detection function based on a structure pointer and a cross-boundary detection logic; wherein the structure pointer includes a structure of all parameters required for memory cross-boundary detection; the cross-boundary detection logic includes a cross-boundary judgment logic corresponding to each parameter in the structure pointer; and an automatic plug-in module 200, which is used to identify memory access operations in a program during compilation, and insert the memory cross-boundary detection function into each of the memory access operations to obtain a target program, so as to detect memory cross-boundary behavior in a real-time operating system based on the target program. Compared with the current manual plug-in that omits certain memory operation blocks, resulting in low accuracy of memory cross-boundary detection, the present invention automatically inserts the designed memory cross-boundary detection function according to the memory operation during the compilation process, so that the R&D personnel do not need to manually write the plug-in code, improve the efficiency of insertion and the accuracy of plug-in, prevent the omission of certain memory operation blocks, thereby improving the accuracy of memory cross-boundary detection, and can directly detect based on the memory cross-boundary detection function, without relying on the runtime library, avoiding the trouble caused by the version and compatibility issues of the library.

[0172] The following is an introduction to a memory cross-border detection device provided by an embodiment of the present invention. The memory cross-border detection device described below and the memory cross-border detection method described above can be referenced to each other.

[0173] Fig. 9 A schematic diagram of a memory cross-border detection device provided by an embodiment of the present invention may include:

[0174] An event receiving module 300 is used to receive events of running a real-time operating system;

[0175] The memory out-of-bounds detection module 400 is used to start the real-time operating system and detect the memory out-of-bounds behavior in the real-time operating system based on the target program to obtain the memory out-of-bounds detection result; wherein the target program is a program obtained based on the above-mentioned memory out-of-bounds detection implementation method.

[0176] It should be noted that the order of the modules and units in the above-mentioned memory cross-border detection device can be changed without affecting the logic.

[0177] Fig. 9 The description of the features in the corresponding embodiments can be found in Fig. 9 The relevant descriptions of the corresponding embodiments will not be repeated here one by one.

[0178] An electronic device provided by an embodiment of the present invention is introduced below. The electronic device described below and the implementation method and memory cross-border detection method described above can refer to each other.

[0179] Fig.10 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention is shown in FIG. Fig.10 As shown, the electronic device may include: a memory 60 for storing a computer program;

[0180] The processor 61 is used to implement the memory cross-border detection implementation method and the steps of the memory cross-border detection method in the above embodiment when executing a computer program.

[0181] The electronic device provided in this embodiment may include but is not limited to a smart phone, a tablet computer, a laptop computer or a desktop computer.

[0182] Among them, the processor 61 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 61 can be implemented in at least one hardware form of digital signal processing (DSP), field-programmable gate array (FPGA), and programmable logic array (PLA). The processor 61 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 61 may be integrated with a graphics processing unit (GPU), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 61 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.

[0183] The memory 60 may include one or more computer-readable storage media, which may be non-transitory. The memory 60 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 60 is at least used to store the following computer program 601, wherein, after the computer program is loaded and executed by the processor 61, it can implement the implementation method of memory cross-border detection and the related steps of the memory cross-border detection method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 60 may also include an operating system 602 and data 603, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 602 may include Windows, Unix, Linux, etc. Data 603 may include but is not limited to the implementation method of memory cross-border detection and the data required for the memory cross-border detection method, etc.

[0184] In some embodiments, the electronic device may further include a display screen 62 , an input / output interface 63 , a communication interface 64 , a power supply 65 , and a communication bus 66 .

[0185] Those skilled in the art will understand that Fig.10 The structure shown in the figure does not constitute a limitation on the electronic device, and may include more or fewer components than shown in the figure.

[0186] It is understandable that if the implementation method of memory cross-border detection and the memory cross-border detection method in the above-mentioned embodiment are implemented in the form of a software functional unit and sold or used as an independent product, they can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention is essentially or the part that contributes to the current technology or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium to execute all or part of the steps of the methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (RandomAccess Memory, RAM), electrically erasable programmable ROM, register, hard disk, removable disk, CD-ROM, magnetic disk or optical disk and other media that can store program codes.

[0187] Based on this, an embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the implementation method and steps of the memory out-of-bounds detection method as described above are implemented.

[0188] The above is a detailed introduction to a method for implementing memory out-of-bounds detection and a method for memory out-of-bounds detection provided by an embodiment of the present invention. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the various embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description.

[0189] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0190] The above is a detailed introduction to a method for implementing memory out-of-bounds detection and a method for memory out-of-bounds detection provided by the present invention. This article uses specific examples to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, the present invention can also be improved and modified, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.

Claims

1. A method for implementing memory cross-bounds detection, characterized in that: include: Generate a memory out-of-bounds detection function based on a structure pointer and an out-of-bounds detection logic; wherein the structure pointer includes a structure of all parameters required for memory out-of-bounds detection; and the out-of-bounds detection logic includes an out-of-bounds judgment logic corresponding to each parameter in the structure pointer; During compilation, memory access operations in the program are identified, and the memory cross-border detection function is inserted into each of the memory access operations to obtain a target program, so as to detect memory cross-border behaviors in a real-time operating system based on the target program.

2. The method for implementing memory cross-bounds detection according to claim 1, characterized in that: The structure pointer includes array out-of-bounds access, pointer arithmetic error, string operation error and structure array out-of-bounds access; wherein, the array out-of-bounds access means that when accessing the array, the accessed index exceeds the set valid range; the pointer arithmetic error means that when performing pointer operation, the calculated address points to an illegal memory area.

3. The method for implementing memory cross-bounds detection according to claim 2, characterized in that: The memory out-of-bounds detection function is generated based on the structure pointer and the out-of-bounds detection logic, including: Determine that the structure pointer is an out-of-bounds access to the array, and the corresponding out-of-bounds detection logic is whether there is an array out-of-bounds access; Determine that the structure pointer is the pointer arithmetic error, and the corresponding out-of-bounds detection logic is whether there is a pointer arithmetic error; Determine that the structure pointer is the string operation error, and the corresponding out-of-bounds detection logic is whether there is a string buffer overflow; Determine that the structure pointer is out of bounds for the structure array, and the corresponding out-of-bounds detection logic is whether there is a structure array out of bounds; The memory out-of-bounds detection function is generated based on each of the structure pointers and the corresponding out-of-bounds detection logic.

4. The method for implementing memory cross-border detection according to any one of claims 1 to 3, characterized in that: Also includes: Generate a memory out-of-bounds error reporting function based on an error reporting parameter structure; wherein the error reporting parameter structure is a structure for determining an error type and an error location; Accordingly, during compilation, memory access operations in the program are identified, and the memory out-of-bounds detection function is inserted into each of the memory access operations to obtain a target program, including: During compiling, the memory access operations in the program are identified, and the memory cross-boundary detection function and the memory cross-boundary error reporting function are inserted into each of the memory access operations to obtain the target program.

5. The method for implementing memory cross-border detection according to claim 4, characterized in that: During compiling, the memory access operation in the program is identified, and the memory out-of-bounds detection function and the memory out-of-bounds error reporting function are inserted into each of the memory access operations. After obtaining the target program, the method further includes: The real-time operating system including the target program is run to obtain a memory cross-boundary detection result and a memory cross-boundary error report.

6. The method for implementing memory cross-bounds detection according to claim 5, characterized in that: When the error report parameter structure is a structure including an error type parameter, an array index parameter where the error occurred, an array size parameter, a storage call stack address parameter, a call stack size parameter, a file name parameter where the error occurred, and a line number parameter where the error occurred; The step of running the real-time operating system including the target program to obtain a memory cross-bounds detection result and a memory cross-bounds error report includes: Running the real-time operating system including the target program to obtain the memory cross-border detection result and the memory cross-border error report including the process number, error type, accessed array index, array size, error occurrence location and call stack information; The process number is used to locate the running instance where the error occurs; The error type is used to determine the type of memory error; The accessed array index is used to determine the accessed array subscript; The array size is used to determine whether an out-of-bounds access occurs; The error occurrence location is used to determine the file name, line number and code location corresponding to the code where the memory out-of-bounds problem occurs; The call stack information is used to determine the call path of the target program when an error occurs.

7. The method for implementing memory cross-bounds detection according to claim 1, characterized in that: The method of identifying memory access operations in a program during compilation and inserting the memory out-of-bounds detection function into each of the memory access operations to obtain a target program, so as to detect memory out-of-bounds behavior in a real-time operating system based on the target program, includes: Determine the risk level of each memory out-of-bounds type. Determine the cross-border detection timing corresponding to each cross-border risk level setting; wherein the higher the cross-border risk level, the greater the corresponding cross-border detection frequency; The memory out-of-bounds detection function, the out-of-bounds risk level and the out-of-bounds detection timing are inserted into each of the memory access operations to obtain the target program.

8. A memory cross-border detection method, characterized in that: include: Receive events from running real-time operating systems; Starting the real-time operating system, and detecting the memory cross-border behavior in the real-time operating system based on the target program to obtain a memory cross-border detection result; Wherein, the target program is a program obtained based on the implementation method of memory out-of-bounds detection described in any one of claims 1 to 7.

9. A device for implementing memory cross-bounds detection, characterized in that: include: A memory out-of-bounds detection function generation module, used to generate a memory out-of-bounds detection function based on a structure pointer and an out-of-bounds detection logic; wherein the structure pointer includes a structure of all parameters required for memory out-of-bounds detection; the out-of-bounds detection logic includes an out-of-bounds judgment logic corresponding to each parameter in the structure pointer; The automatic plug-in module is used to identify memory access operations in a program during compilation, and insert the memory out-of-bounds detection function into each of the memory access operations to obtain a target program, so as to detect memory out-of-bounds behavior in a real-time operating system based on the target program.

10. A memory cross-border detection device, characterized in that: include: An event receiving module, used for receiving events of running the real-time operating system; A memory cross-border detection module is used to start the real-time operating system and detect the memory cross-border behavior in the real-time operating system based on the target program to obtain a memory cross-border detection result; Wherein, the target program is a program obtained based on the implementation method of memory out-of-bounds detection described in any one of claims 1 to 7.

11. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, used to execute the computer program to implement the method for implementing memory cross-border detection as described in any one of claims 1 to 7, and the steps of the memory cross-border detection method as described in claim 8.

12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for implementing memory out-of-bounds detection as described in any one of claims 1 to 7 and the steps of the memory out-of-bounds detection method as described in claim 8 are implemented.