Memory exception detection method and electronic device
By setting memory regions with restricted access states for objects on terminal devices, the incompatibility issue of memory access anomaly detection caused by recompilation in existing technologies is resolved, realizing memory anomaly detection without compilation and improving system stability and operating efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HUAWEI TECH CO LTD
- Filing Date
- 2024-12-18
- Publication Date
- 2026-06-19
AI Technical Summary
Existing technologies require recompiling programs when detecting abnormal memory access on terminal devices, which leads to incompatibility with operating system code, prevents normal operation, and affects system stability and user experience.
By setting a first memory region under restricted access for each object, memory access exceptions can be detected, avoiding program recompilation. The virtual machine monitors the memory access process and obtains stack information in a timely manner to locate exceptions.
Without increasing operating system management costs and space usage, this method enables the detection of memory access anomalies, improving system stability and operating efficiency, and avoiding detection failures caused by code incompatibility.
Smart Images

Figure CN122240365A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of terminal technology, and in particular to a memory anomaly detection method and electronic device. Background Technology
[0002] During the iterative development of terminal device operating systems, memory access anomalies remain a primary concern in system stability testing. In actual operation, if critical system programs and / or applications deployed within the operating system experience memory access anomalies, it can potentially lead to a device restart, thereby disrupting normal user experience and severely impacting the user's experience.
[0003] Currently, to achieve real-time monitoring of memory access anomalies, memory monitoring tools (addresssanitizer, ASan) are typically used to monitor the program's memory access process. ASan performs dynamic instrumentation during program execution, using additional compiled code to determine the program's memory usage status. However, using ASan for memory monitoring requires recompiling the program before ASan can be deployed. This additional compilation can lead to code incompatibility issues between ASan and the operating system, causing ASan to fail to function properly within the operating system. Summary of the Invention
[0004] This application provides a memory anomaly detection method and electronic device, which can detect memory access anomalies of the first program without recompiling it, by means of a first memory region in memory space that is set for each object and is in a restricted access state. Thus, it can avoid the inability to detect memory access anomalies due to code incompatibility issues.
[0005] To achieve the above objectives, the embodiments of this application adopt the following technical solutions:
[0006] Firstly, a memory anomaly detection method is provided. In this method, firstly, an electronic device acquires at least one object corresponding to a first program. Secondly, the electronic device sets a first memory region for each object. Then, during the execution of the first program, if the electronic device detects that the first program requests access to the first memory region, it determines that the first program has encountered a memory access anomaly.
[0007] In this way, during the operation of the first program, the electronic device can use the first memory area set for each object corresponding to the first program to detect whether there is a memory access exception in the first program. Thus, the memory access exception of the first program can be detected without compiling the first program.
[0008] The object is used to store the program data of the first program; at least one object is stored in memory space; the first memory region and the object occupy contiguous memory space; the first memory region is in a restricted access state.
[0009] In one possible implementation of the first aspect, after determining that a memory access exception has occurred in the first program, the electronic device obtains the stack information of the first program in order to locate the location of the memory access exception by analyzing the stack information.
[0010] The stack trace information is used to indicate the location of the memory access exception in the first program.
[0011] In one possible implementation of the first aspect, when the electronic device sets up the first memory region of an object, during the memory allocation phase, the electronic device allocates the first memory region for the object. Alternatively, during the memory reclamation phase, the coverage of the first memory region in the memory space is adjusted. Alternatively, during the memory reclamation phase, the storage location of the object and the object's first memory region in the memory space is adjusted.
[0012] Here, the electronic device sets up a configuration suitable for each stage of the object's existence, thereby enabling flexible adjustment of the first memory area during the object's lifespan.
[0013] In one possible implementation of the first aspect, when the electronic device allocates a first memory region to an object, the electronic device allocates a first memory region and a region identifier to the object, so as to represent the size of the first memory region by setting the region identifier. The setting of the region identifier can also ensure that the first memory region is set according to the "4K sector rule". That is, the memory space allocated to the object by the operating system is consistent with the number of bytes written by the internal memory. Thus, when the virtual machine reads and / or writes data, the data reading and / or writing can be completed at one time, thereby ensuring the hard disk read and write efficiency.
[0014] The region identifier is used to indicate the size of the memory space occupied by the first memory region; the object, the first memory region, and the region identifier occupy contiguous memory space.
[0015] In one possible implementation of the first aspect, when the electronic device adjusts the coverage of the first memory region in the memory space, if the object is an object to be reclaimed, the electronic device adjusts the coverage of the first memory region in the memory space according to the memory reclamation method corresponding to the object to be reclaimed. Thus, during the memory reclamation phase, the electronic device can select a memory reclamation method suitable for the object to be reclaimed to realize the reclamation of the object's memory space, thereby improving the flexibility of the memory reclamation process.
[0016] In one possible implementation of the first aspect, when the electronic device adjusts the storage location of the object and the object's first memory region in the memory space, if the object is a non-reclaimable object, the virtual machine adjusts the storage location of the object and the object's first memory region in the memory space according to the memory reclamation method corresponding to the non-reclaimable object. Thus, during the memory reclamation phase, the electronic device can select a memory reclamation method suitable for the non-reclaimable object, retaining the object's occupation of the memory space, thereby avoiding blindly releasing the memory space it occupies, which could cause the first program to fail to run.
[0017] In one possible implementation of the first aspect, when the electronic device adjusts the coverage of the first memory region in the memory space according to the memory reclamation method corresponding to the object to be reclaimed, when the memory reclamation method is the first method, the coverage of the first memory region in the memory space is expanded; when the memory reclamation method is the second method, the memory space occupied by the object and the first memory region is released.
[0018] It is evident that when adjusting the coverage area of the first memory region in an electronic device, different memory reclamation methods can be combined with different adjustment methods to achieve the adjustment of the coverage area of the first memory region, thereby enriching the adjustment methods of the coverage area.
[0019] In one possible implementation of the first aspect, after the electronic device expands the coverage of the first memory region in the memory space, the resulting adjusted first memory region covers the memory region occupied by the object, the original first memory region, and the memory region occupied by the region identifier.
[0020] In one possible implementation of the first aspect, when the electronic device adjusts the storage location of the object and its first memory region in memory space according to the memory reclamation method corresponding to the non-reclaimed object, if the memory reclamation method is a third method, the electronic device maintains the storage location of the object and its first memory region in memory space. If the memory reclamation method is a fourth method, the electronic device moves the object and its first memory region in memory space to adjust the storage location of the object and its first memory region in memory space. In another possible implementation of the first aspect, if the fourth method is a copy method, the memory space includes a first subspace and a second subspace, and the object and its first memory region are stored in the first subspace; when the electronic device moves the object and its first memory region, firstly, the electronic device copies multiple objects together to the second subspace and releases the first subspace occupied by each object and its first memory region. Secondly, for each object stored in the second subspace, the electronic device resets the first memory region for that object in the second subspace, thereby adjusting the storage location of the object and its first memory region in memory space. Furthermore, after adjusting the storage location of the object, not only can a contiguous free memory area be obtained to the greatest extent, but the first memory area can also be used to detect whether there is a memory access exception in the first program.
[0021] Among them, the object and the object's first memory region occupy a contiguous second subspace.
[0022] In one possible implementation of the first aspect, if the fourth method is a tag compression method, when the electronic device moves objects and their first memory regions, firstly, the electronic device centrally stores multiple objects in memory space and releases the original memory space occupied by the first memory region of each object. Secondly, for each object after central storage, the electronic device resets its first memory region, thereby adjusting the storage location of the object and its first memory region in memory space. Furthermore, after adjusting the object's storage location, not only can contiguous free memory regions be obtained to the greatest extent possible, but the first memory region can also continue to be utilized to detect whether the first program has memory access anomalies.
[0023] In one possible implementation of the first aspect, after the electronic device expands the coverage of the first memory region in the memory space to obtain the adjusted first memory region, the electronic device releases the adjusted first memory region at the memory release time to avoid the problem of memory leakage caused by long-term occupation of memory space when the first memory region cannot be accessed.
[0024] In one possible implementation of the first aspect, the first program requests access to the first memory region, including: the first program requests to write data to the first memory region; or, the first program requests to read data from the first memory region.
[0025] In one possible implementation of the first aspect, the first method is either a mark-sweeping method or a copying method; the second method is a mark-compression method.
[0026] In one possible implementation of the first aspect, the third method is a mark-clearing method; the fourth method is either a copying method or a mark-compression method.
[0027] In a second aspect, an electronic device is provided, the electronic device including a memory and one or more processors; the memory is coupled to the processors; wherein the memory stores computer program code, the computer program code including computer instructions, and when the computer instructions are executed by the processor, the electronic device performs the memory anomaly detection method as described in the first aspect and any implementation thereof.
[0028] Thirdly, a computer-readable storage medium is provided, including computer instructions that, when executed on an electronic device, cause the electronic device to perform a memory anomaly detection method as described in the first aspect and any of its implementations.
[0029] Fourthly, a computer program product is provided that, when the computer program product is run on an electronic device, causes the electronic device to execute the memory anomaly detection method as described in the first aspect and any of its implementations.
[0030] The beneficial effects that the electronic device provided in the second aspect, the computer-readable storage medium provided in the third aspect, and the computer program product provided in the fourth aspect can achieve are similar to the beneficial effects that can be achieved in the first aspect and any of its implementations, and will not be repeated here. Attached Figure Description
[0031] Figure 1 This illustration shows a schematic diagram of a memory detection process provided in an embodiment of this application;
[0032] Figure 2 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of this application is shown;
[0033] Figure 3 A schematic diagram of the software structure of an electronic device provided in an embodiment of this application is shown;
[0034] Figure 4 A flowchart of a memory anomaly detection method provided in an embodiment of this application is shown;
[0035] Figure 5 This illustration shows a schematic diagram of a first memory region partitioning provided in an embodiment of this application;
[0036] Figure 6 This illustration shows a schematic diagram of the location of a first memory region provided in an embodiment of this application;
[0037] Figure 7 This illustration shows a schematic diagram of a memory anomaly alarm process provided in an embodiment of this application;
[0038] Figure 8 This illustration shows a schematic diagram of a memory access process provided in an embodiment of this application;
[0039] Figure 9 This illustration shows a schematic diagram of the location of a region identifier provided in an embodiment of this application;
[0040] Figure 10 This illustration shows a schematic diagram of the coverage area of a first memory region provided in an embodiment of this application;
[0041] Figure 11 This illustration shows a schematic diagram of a memory region coverage adjustment process provided in an embodiment of this application;
[0042] Figure 12 This illustration shows another memory region coverage adjustment process provided in an embodiment of this application;
[0043] Figure 13 This illustration shows another memory region coverage adjustment process provided in an embodiment of this application;
[0044] Figure 14 A schematic diagram of the hardware structure of another electronic device provided in an embodiment of this application is shown. Detailed Implementation
[0045] The technical solutions of the embodiments of this application will be described below with reference to the accompanying drawings. In the description of this application, unless otherwise stated, " / " indicates that the objects before and after are in an "or" relationship. For example, A / B can represent A or B. "And / or" in this application is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, and B alone, where A and B can be singular or plural. Furthermore, in the description of this application, unless otherwise stated, "multiple" refers to two or more. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, and c can be single or multiple. Furthermore, to facilitate a clear description of the technical solutions in the embodiments of this application, the terms "first" and "second" are used in the embodiments of this application to distinguish identical or similar items with substantially the same function and effect. Those skilled in the art will understand that the terms "first" and "second" do not limit the quantity or execution order, and that "first" and "second" are not necessarily different. Meanwhile, in the embodiments of this application, the terms "exemplary" or "for example" are used to indicate that something is being used as an example, illustration, or description. Any embodiment or design scheme described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design schemes. Specifically, the use of terms such as "exemplary" or "for example" is intended to present related concepts in a concrete manner for ease of understanding.
[0046] Furthermore, the business scenarios described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided by the embodiments of this application. As those skilled in the art will know, with the emergence of new business scenarios, the technical solutions provided by the embodiments of this application are also applicable to similar technical problems.
[0047] During the iterative development of terminal device operating systems, memory access anomalies remain a primary concern in system stability testing. In actual operation, if critical system programs and / or applications deployed within the operating system experience memory access anomalies, it can very likely lead to a direct restart of the terminal device.
[0048] When an operating system encounters a memory access exception, it's necessary to analyze the stack traces generated by system programs and / or applications to pinpoint the location of the exception. Memory access exceptions are typically low-probability events, so the operating system and / or technical personnel don't frequently retrieve stack traces to detect them during program execution. However, stack traces are "victims"—the information generated when a memory access exception occurs during program execution, only recording the location of the current exception. The root cause of this exception is often a previous memory access exception (e.g., a memory access exception caused by another program writing data to the program's memory). Therefore, if the memory access exception isn't detected promptly, it may be impossible to obtain stack traces that pinpoint its root cause.
[0049] To achieve real-time detection of memory access exceptions, a built-in memory monitoring tool (AddressSanitizer, ASan) is typically used. During program execution, ASan monitors the program's memory access process. ASan is primarily used in C / C++ program development. It detects memory access exceptions during program execution by dynamically instrumenting the memory space used by the program, such as buffer overflows, use of uninitialized memory, and continued access after memory has been freed.
[0050] ASan's detection principle mainly includes the following aspects:
[0051] (1) Setting up Memory Shadows: Before implementing memory access exception detection, ASAn allocates an "extra memory region" of the same size for each memory region occupied by the program in the memory space. The "extra memory region" is regarded as the memory shadow of that memory region. The purpose of the memory shadow is to record the state of each byte in the memory region and whether that byte has been accessed. When memory is allocated, ASAn sets the state of the memory shadow to unaccessed, and as the program runs, the state of the memory shadow is set to accessed. When other programs request access to the memory region, ASAn will first check the state of the memory shadow of that memory region. If it finds that the state of the memory shadow has been accessed, it determines that there is a memory access exception problem during the access process of other programs.
[0052] (2) Redzone setting: ASAn adds a "red zone" to the boundary of each memory region occupied by the program. The "red zone" is actually a segment of unallocated memory in the memory space. The setting of the "red zone" is used to detect buffer overflow exceptions. During program execution, when the program attempts to access the "red zone", ASAn will immediately detect it and determine that there is a memory access exception problem.
[0053] (3) The process of compiler instrumentation: If ASaan wants to monitor the memory access process and update the memory shadow state, it needs to use the compiler to perform instrumentation operations. By recompiling the program, some additional code is added to the program to realize the deployment of "memory shadow" and "red zone" in memory space, the monitoring of memory access process, and the updating of memory shadow state.
[0054] In summary, using ASAn to detect memory access exceptions requires recompiling the program. This recompilation process inevitably increases the amount of code that needs to be executed during program runtime. Therefore, using ASAn increases the operating system's management and space usage costs, reducing program speed. Furthermore, the deployment method of the additional compiled code can lead to code incompatibility issues between ASAn and the operating system and / or the program, causing ASAn to fail to function properly within the operating system and thus preventing the detection of memory access exceptions during program execution.
[0055] Based on the above, this application provides a memory anomaly detection method. First, at least one object involved in the execution of a first program is obtained, wherein at least one object is stored in memory space. Second, for each object, a first memory region is set up for that object. The first memory region occupies contiguous memory space with the object and is in a restricted access state, so as to detect whether there is a memory anomaly in the memory region covered by the object through the first memory region. Finally, during the execution of the first program, if the first program requests access to the first memory region of the object, a memory access anomaly of the first program is determined. In this way, during the execution of the first program, there is no need to recompile the first program. By using the additional first memory region set up for each object and in a restricted access state, the memory access anomaly detection of the first program can be achieved, thereby avoiding the inability to detect memory access anomalies due to code incompatibility.
[0056] Furthermore, since this application does not require compiling the first program during the memory access exception detection process, it can detect memory access exceptions without increasing the amount of code run by the operating system. Consequently, it does not increase the management cost and space usage cost of the operating system, thus avoiding a decrease in program running speed.
[0057] This application provides a memory anomaly detection method that can be applied to electronic devices, including operating systems, which in turn include virtual machines. The virtual machine can specifically implement this method. For example,... Figure 1 As shown, when the first program (e.g., a system program and / or an application program) runs in the operating system, the virtual machine in the operating system is responsible for managing the memory space occupied by the first program, including at least: allocating memory space for storing program data for the first program, and reclaiming the memory space occupied by the first program. During the execution of the first program, the virtual machine monitors the memory access process of the first program. If the virtual machine detects that the first program attempts to access a first memory region that is in a restricted access state, it reports a memory access exception to the operating system. Furthermore, when a memory access exception occurs, it promptly obtains the stack information of the first program.
[0058] The operating system runs within an electronic device and controls and manages its hardware and software resources to enable the device's functions and facilitate human-computer interaction. The first programs installed in the operating system can include system programs and / or application programs. System programs are those that support the operation of the operating system, such as language processing programs and network management programs. Application programs are programs compiled using programming languages to meet user application needs, such as programs in game software and office software.
[0059] The electronic device 100 involved in the memory anomaly detection method provided in this application embodiment can be found in [reference needed]. Figure 2 As shown. Electronic device 100 may include processor 110, external memory interface 120, internal memory 121, universal serial bus (USB) interface 130, charging management module 140, power management module 141, battery 142, antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, etc.
[0060] It is understood that the structures illustrated in the embodiments of this application do not constitute a specific limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements.
[0061] Processor 110 may include one or more processing units, such as: application processor (AP), modem processor, graphics processing unit (GPU), image signal processor (ISP), controller, memory, video codec, digital signal processor (DSP), baseband processor, and / or neural network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.
[0062] The controller can be the nerve center and command center of the electronic device 100. The controller can generate operation control signals according to the instruction opcode and timing signals to complete the control of fetching and executing instructions.
[0063] The processor 110 can implement the aforementioned memory anomaly detection method. The processor 110 may also include a memory, the memory space of which is used to store program data involved in the first program. In some embodiments, the memory in the processor 110 is a cache memory. This memory can store program data that the processor 110 has just used or that is used repeatedly. If the processor 110 needs to use the program data of the first program multiple times, it can directly retrieve it from the memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the operating system.
[0064] In some embodiments, the processor 110 may include one or more interfaces. Different interface connection methods or combinations of multiple interface connection methods may be used between the interfaces.
[0065] The charging management module 140 receives charging input from a charger. The charger can be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 receives charging input from the wired charger via the USB interface 130. In some wireless charging embodiments, the charging management module 140 receives wireless charging input via the wireless charging coil of the electronic device 100. While charging the battery 142, the charging management module 140 can also supply power to the electronic device via the power management module 141.
[0066] The power management module 141 connects the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, and supplies power to the processor 110, internal memory 121, external memory, wireless communication module 160, etc. The power management module 141 can also monitor parameters such as battery capacity, battery cycle count, and battery health status (leakage current, impedance). In some other embodiments, the power management module 141 may also be located within the processor 110. In other embodiments, the power management module 141 and the charging management module 140 may be located in the same device.
[0067] The wireless communication function of electronic device 100 can be realized through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem processor and baseband processor, etc.
[0068] The external storage interface 120 can be used to connect an external storage card, such as a Micro SD card, to expand the memory space of the electronic device 100. The external storage card communicates with the processor 110 through the external storage interface 120 to store program data for the first program. For example, program data for applications such as music and video can be stored on the external storage card.
[0069] Internal memory 121 can be used to store computer executable program code, and the program data for executing the first program includes instructions. Processor 110 executes various functional applications and data processing of electronic device 100 by running the program data stored in internal memory 121. Internal memory 121 may include a program storage area and a data storage area. The program storage area may store the operating system, at least one system program required for a function (such as the desktop, control bar, etc.), and / or application programs (such as sound playback function, image playback function, etc.). The data storage area may store data created during the use of electronic device 100 (such as audio data, phonebook, etc.). Furthermore, internal memory 121 may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc.
[0070] The electronic device provided in this application embodiment can run an operating system (OS). This operating system can be various operating systems used in the industry, such as an operating system based on OpenHarmony, like HarmonyOS; or other operating systems such as Android. TMAn operating system can refer to the iOS mobile operating system; it can also refer to various open-source operating systems or their derivatives, such as Linux OS and other embedded operating systems; or it can refer to future new operating systems, such as AI operating systems based on artificial intelligence. An operating system is a set of interconnected system software programs that manage and control the operation of electronic devices, utilize and run hardware and software resources, and provide public services to organize user interactions. In electronic devices, the operating system connects downwards to the physical devices at the hardware layer and upwards to provide a runtime environment for application software.
[0071] An operating system typically includes a kernel layer, a middleware layer, and an application layer. The application layer includes applications, which can include system applications and third-party applications. The middleware layer includes a suite of software providing various services to application developers, or frameworks providing services such as databases, multimedia, and graphics, or capabilities such as distributed scheduling and system scaling. For example, the middleware layer may include a framework layer and / or a system service layer. The framework layer provides application programming interfaces (APIs) and programming frameworks for applications in the application layer. The system service layer includes the system's core capabilities, providing services to applications through the framework layer. The kernel layer is the layer between hardware and software. The kernel layer may include hardware drivers and the operating system kernel. In addition to providing hardware drivers, the kernel layer also supports functions such as memory management and system process management.
[0072] The electronic devices we use in our daily lives come in various types and forms, and are applied in a wide range of scenarios. Therefore, based on the different forms and functions of electronic devices, different application scenarios, and different user needs, the operating systems used in these devices may also differ. The basic functions implemented by the electronic device provided in this application can be implemented using a general-purpose operating system or a dedicated operating system. To more clearly illustrate the implementation of the embodiments of this application under a specific operating system, the architecture of HarmonyOS is shown below. Those skilled in the art can deduce the implementation of the embodiments of this application under other specific operating systems, such as Android. TM Implementation under operating systems, etc.
[0073] The software architecture of an electronic device can be divided into several layers. In some embodiments, such as... Figure 3 As shown, from bottom to top, the layers are: kernel layer, system service layer, framework layer, and application layer. Layers communicate with each other through software interfaces. System functions can be tailored, added, or combined at the subsystem level depending on the deployment scenario of different device types, and each subsystem can also be tailored, added, or combined at the functional level.
[0074] The kernel layer includes, but is not limited to: the kernel abstract layer (KAL), the kernel subsystem, and the driver subsystem.
[0075] The Kernel Abstraction Layer (KAL) provides basic kernel capabilities to upper layers by shielding the differences between multiple kernels, including but not limited to process / thread management, memory management, file system, network management, and peripheral device management.
[0076] The kernel subsystem supports selecting a suitable OS kernel for different resource-constrained devices, not limited to HarmonyOS kernel, LiteOS (Lite Operating System), etc.
[0077] Driver Subsystem: The driver framework is the foundation for the open system hardware ecosystem, providing unified peripheral access capabilities and a framework for driver development and management. The driver framework includes: display drivers, camera drivers, audio drivers, Bluetooth drivers, sensor drivers, etc.
[0078] The system service layer comprises the core capabilities of the system, providing services to applications through the framework layer. This layer involves multiple subsystem sets, including but not limited to: basic system capability subsystem set, basic software service subsystem set, enhanced software service subsystem set, and hardware service subsystem set.
[0079] The system's basic capability subsystem set provides the foundational capabilities for the operation, scheduling, and migration of distributed applications across multiple devices. This set may include distributed soft bus, distributed data management, distributed task scheduling, and Ark multi-language runtime; it may also include multi-modal input subsystem, graphics subsystem, security subsystem, and AI business subsystem.
[0080] The basic software service subsystem set provides public and general software services; the basic software service subsystem set may include an event notification subsystem, a telephone subsystem, a multimedia subsystem, etc.
[0081] The enhanced software service subsystem suite provides differentiated enhanced software services for different devices; the enhanced software service subsystem suite may include smart screen proprietary business subsystem, wearable proprietary business subsystem, IoT proprietary business subsystem, etc.
[0082] The hardware service subsystem set provides hardware services; the hardware service subsystem set may include a location service subsystem, a unified identity and access management (IAM) subsystem, a wearable proprietary hardware service subsystem, biometric identification, IoT proprietary hardware services, and other subsystems.
[0083] Distributed task scheduling enables distributed service management (discovery, synchronization, registration, and invocation), supporting remote startup, remote invocation, remote connection, and migration of applications across devices.
[0084] Distributed data management enables data synchronization, data storage, data sharing, and data access across all scenarios and devices.
[0085] The distributed soft bus provides communication-related capabilities for seamless interconnection between multiple devices, including: WLAN service capabilities, Bluetooth service capabilities, soft bus, inter-process communication (remote procedure call, RPC), and StarFlash communication capabilities.
[0086] Ark Multilingual Runtime is a unified compilation runtime platform designed to support the joint compilation and execution of multiple programming languages and multiple chip platforms.
[0087] The framework layer provides application programming interfaces (APIs) and programming frameworks for applications in the application layer. The framework layer includes: the ArkUI framework (which provides a complete infrastructure for UI development of system applications, including UI functions such as components, layouts, animations, and interactive events, as well as a real-time interface preview tool), the user application framework, and the Ability framework (an Ability is a lightweight application; the Ability framework schedules and manages the operation and lifecycle of Abilities). Different devices may have different operating systems, and the APIs they support may also differ.
[0088] The HarmonyOS API is a series of open capabilities provided to support HarmonyOS application development. The HarmonyOS API can be set at the framework layer or independently of the framework layer. The HarmonyOS API includes the Audio API (audio service), Push API (push service), and Account API (account service), among others.
[0089] The application layer includes the first program, which can include system programs and applications (extended applications / third-party applications). System programs can include the desktop, control bar, settings, contacts, phone, camera, etc., while extended applications / third-party applications can include social applications, travel applications, etc.
[0090] The following description uses a virtual machine in an electronic device executing the aforementioned memory anomaly detection method as an example to illustrate the memory anomaly detection method provided in this application embodiment. The virtual machine (Runtime) is set within the operating system and is part of the operating system. The virtual machine (Runtime) is responsible for executing and managing the runtime environment of applications. In some examples, the virtual machine (Runtime) can be considered a runtime library, providing various core functions, such as memory allocation, thread management, and memory reclamation. Thus, under the operation of the virtual machine (Runtime), it can be ensured that system programs and applications can run correctly and efficiently on the electronic device.
[0091] like Figure 4 As shown, the method may include the following steps S401-S403.
[0092] S401, The virtual machine obtains at least one object corresponding to the first program.
[0093] In this context, an object serves as a data storage container, used to store program data involved in the execution of the first program. At least one object involved in the first program is stored in memory space.
[0094] For example, the program data of the first program may include: the program code of the first program, the code parameters involved in the program code, the running logs generated during the operation of the first program, the functions called in the process of providing services to users, and the account information of users using the first program, etc.
[0095] In this context, at least one object is stored in memory, meaning that each object occupies a different memory region. This memory region can be divided in at least two ways:
[0096] Firstly, based on the size of the objects, for example, the execution of the first program involves two objects: object 1 and object 2. Object 1 stores more program data, while object 2 stores less. In this case, a larger memory region 1 can be allocated to object 1, and a smaller memory region 2 can be allocated to object 2, such as... Figure 5 As shown in (a).
[0097] Second, the memory space is pre-divided into multiple memory regions proportionally based on its size. For example, the memory space can be divided into memory regions 1 through 5, such as... Figure 5 As shown in (b), memory regions 1 through 5 are the same size.
[0098] S402. During the execution of the first program, the virtual machine sets up a first memory region corresponding to each object of the first program.
[0099] Furthermore, during the execution of the first program, the memory access situation during the execution of the first program can be detected by using the first memory area set for each object, and it can be determined whether a memory access exception occurs when the first program accesses the memory space occupied by each object.
[0100] Furthermore, since each object involved in the execution of the first program is provided with a corresponding first memory region in this embodiment, comprehensive detection of the memory space involved in the first program can be achieved during the execution of the first program.
[0101] There is a unique correspondence between objects and the first memory region. Each first memory region can only be used to detect whether there is a memory access exception during the access of the first program to its corresponding object, provided that the first program accesses the memory space of the object it corresponds to. If the first program involves multiple objects 1 to 3, the first memory region 1 is set up separately from the corresponding object 1, such as... Figure 6 As shown in (a), even if the first program erroneously accesses the first memory region 1, it is impossible to determine which object the first program accessed when the memory access exception occurred. Therefore, when the virtual machine sets up a first memory region for each object, it allocates contiguous memory space for that object and its corresponding first memory region, thus indicating the correspondence between the object and the first memory region; that is, the first memory region 1 and its corresponding object 1 need to occupy contiguous memory space, such as... Figure 6 As shown in (b).
[0102] The first memory region is in a restricted access state. Restricted access means that the first memory region is set to a format that restricts reading and writing data. In this state, if the first program requests to read data from or write data to the first memory region, it indicates that the first program is accessing an illegal memory address. Therefore, the virtual machine can determine that the first program has a memory access exception.
[0103] S403. During the execution of the first program, if the first program requests access to the first memory region, the virtual machine determines that there is a memory access exception problem in the execution of the first program.
[0104] Among them, memory access exception refers to the first program attempting to access an illegal memory address and / or the first program attempting to execute an illegal or undefined instruction during the execution of the first program.
[0105] In some examples, a first program's request to access a first memory region includes at least two scenarios: First, the first program requests to write data to the first memory region. Second, the first program requests to read data from the first memory region.
[0106] In this way, the virtual machine can monitor the first program's access operations, such as reading and / or writing to memory regions, during the program's execution. It can determine whether the requested memory region is the first memory region, thus detecting any memory access anomalies. If the first program requests access to the first memory region, the virtual machine can promptly detect the memory access anomaly and issue an alert. Figure 7 As shown. If the first program requests access to a memory region other than the first memory region, then the first program's access operation is determined to be a normal access.
[0107] In some implementations, after the virtual machine determines that there is a memory access exception during the execution of the first program, the virtual machine further obtains the stack information generated during the execution of the first program. This allows developers to locate the exception location, exception type, and exception execution code number of the memory access exception by analyzing the stack information.
[0108] The stack information contains call data between the heap and stack areas during the execution of the first program, including the location of memory access exceptions in the first program. Memory access exceptions in the stack information can be represented by at least two system signals: Signal 11 and Signal 4.
[0109] Signal 11 (segmentation fault, SIGSEGV) is a signal issued by the virtual machine when the first program attempts to access an illegal memory address. Common memory access exceptions that cause the virtual machine to issue SIGSEGV include:
[0110] Null pointer dereferencing: This refers to the first program attempting to access a memory region pointed to by an uninitialized or already freed pointer;
[0111] Array out of bounds: The program accesses memory beyond the array boundary;
[0112] Stack overflow: Too many recursive function calls cause the stack space to run out, triggering an illegal access;
[0113] Accessing protected memory regions: Attempting to read or write read-only memory regions or kernel-protected memory;
[0114] Dynamic memory management error: such as using memory that has already been freed or double-freeing.
[0115] Signal 4 (illegal instruction, SIGILL) is a signal issued by the virtual machine when a program attempts to execute an illegal or undefined instruction. Common memory access exceptions that can cause the virtual machine to issue a SIGILL signal include:
[0116] Corrupted binary file: The executable file is corrupted due to disk damage or other reasons, and the first program may attempt to execute invalid instructions;
[0117] Instruction set incompatibility: When the first program is running on a CPU architecture that does not support certain instructions;
[0118] Memory corruption: The first program code segment is accidentally modified, causing the first program to attempt to execute the contents of the data segment instead of the actual machine instructions;
[0119] Executing an uninitialized function pointer: The first program jumps to an uninitialized or invalid address (such as a function pointer that has not been correctly assigned a value).
[0120] For an object, its "complete lifecycle" involves at least two phases in terms of memory space usage: the memory allocation phase and the memory reclamation phase. The memory allocation phase refers to the stage of constructing the object and allocating memory space for it; the memory reclamation phase refers to the stage of releasing the object and reclaiming the memory space it occupies. In these different phases, the virtual machine's approach to setting up the object's first memory region also differs.
[0121] The "complete lifecycle" refers to the time period during which an object exists in the first program, including the entire process of object construction, use, and release.
[0122] If the object of the first program is in the memory allocation stage, in some implementations, after the virtual machine completes the allocation of the memory space occupied by the object, it further divides a memory region as the first memory region of the object, thereby making the object and the first memory region of the object occupy contiguous memory space.
[0123] For example, the size of the memory space occupied by the first memory region is positively correlated with the importance of the first program. That is, the higher the importance of the first program, the larger the memory space occupied by the first memory region; conversely, the lower the importance of the first program, the smaller the memory space occupied by the first memory region.
[0124] In some examples, the importance of the first program may depend on at least one of its stability, operational relevance, and complexity. Specifically, the stability of the first program is negatively correlated with its importance, the operational relevance of the first program is positively correlated with its importance, and the complexity of the first program is positively correlated with its importance.
[0125] In some examples, stability depends on the frequency of anomalies occurring in the first program; the higher the frequency of anomalies occurring in the first program, the lower its stability; conversely, the lower the frequency of anomalies occurring in the first program, the higher its stability.
[0126] The operational correlation depends on whether the memory access exception of the first program will affect the operation of the operating system. If the memory access exception of the first program causes the operating system to fail to run, then the operational correlation of the first program is strong; conversely, if the memory access exception of the first program does not affect the operation of the operating system, then the operational correlation of the first program is weak.
[0127] The complexity depends on the amount of data in the first program. If the first program involves a large amount of data, the complexity of the first program is high; conversely, if the first program involves a small amount of data, the complexity of the first program is low.
[0128] The size of the first memory region is positively correlated with the virtual machine's ability to detect memory access anomalies. In other words, the larger the memory space occupied by the first memory region, the stronger the virtual machine's ability to detect memory access anomalies; conversely, the smaller the memory space occupied by the first memory region, the weaker the virtual machine's ability to detect memory access anomalies.
[0129] For example, during the execution of the first program, a memory space 'a' is allocated for object 1 of the first program, such as... Figure 8 As shown in (a). If a smaller first memory region b is allocated for object 1, then during the execution of the first program, it is highly likely that the first program will "skip" the first memory region b and access the memory space c occupied by object 2. Since the access permissions of memory space c are unrestricted, the first program can write and / or read data in memory space c. In this case, although the virtual machine sets up a first memory region b for object 1, the virtual machine cannot utilize the first memory region b to implement memory access anomaly detection for the first program.
[0130] However, if a larger first memory region b' is allocated for object 1, such as Figure 8As shown in (b), during the execution of the first program, the probability of accessing the memory space c occupied by object 2 by "crossing" the first memory region b' is extremely low. At this time, when the first program requests access to the first memory region b', the virtual machine can use the first memory region b' to detect the memory access anomaly of the first program.
[0131] If the object of the first program is in the memory allocation stage, in some other implementations, when the virtual machine allocates the first memory region for the object, the virtual machine divides a memory region from the memory space occupied by the object as the first memory region corresponding to the object. At the same time, it divides another memory region to set a region flag that indicates the size of the memory space occupied by the first memory region.
[0132] The object, the first memory region, and the region identifier occupy contiguous memory space. The region identifier can be located between the object and the first memory region, such as... Figure 9 As shown in (a); it can also be located after the first memory region, such as Figure 9 As shown in (b); it can also be located before the memory area occupied by the object, such as... Figure 9 As shown in (c).
[0133] The setting of the aforementioned region flag not only indicates the size of the first memory region, but also ensures that the first memory region is set according to the "4K sector rule". This ensures that the memory space allocated by the operating system to the object is consistent with the number of bytes written to the internal memory. Consequently, when the virtual machine reads and / or writes data, the data can be read and / or written at once, thereby ensuring hard disk read and write efficiency.
[0134] Here, during the execution of the first program, when the program needs to implement human-computer interaction functions by calling objects, it enters the memory allocation phase. During this phase, the virtual machine constructs an object for the first program and, after completing the object's construction, allocates the memory space it occupies for the constructed object and its initial memory space. As the first program's process progresses, the constructed object is continuously called by the root nodes it references until the constructed object enters the memory reclamation phase.
[0135] During the memory reclamation phase, the virtual machine marks a constructed object based on its references to the first program, classifying it as either a pending reclamation object or a non-reclaimable object. This allows for the targeted release of memory space occupied by pending reclamation objects and the retention of memory space occupied by non-reclaimable objects. During the reclamation phase, the virtual machine uses different configuration methods when setting up the first memory region for non-reclaimable and pending reclamation objects.
[0136] In this context, non-reclaimed objects refer to objects that are still being accessed by the first program and are in a "referenced state." Because the memory space they occupy is still needed by the first program, the memory space they occupy cannot be released to other programs or objects of the first program.
[0137] An object to be garbage collected refers to an object that will no longer be accessed by the first program and is in a "non-referenced state". Since the memory space it occupies will no longer be accessed by the first program, the memory space it occupies can be released to other programs or other objects of the first program.
[0138] If the object of the first program is in the memory reclamation phase, in some implementations, if the object is an object to be reclaimed, the coverage of the object's first memory region in the memory space is adjusted to release the object and the memory space occupied by the object's first memory region.
[0139] Adjusting the coverage of the first memory region in memory space can refer to expanding the coverage of the first memory region from its original extent, for example, expanding the memory space occupied by the object to include the first memory region. Alternatively, it can refer to shrinking the coverage of the first memory region from its original extent to release the memory space it occupies, for example, clearing the first memory region.
[0140] In some examples, when adjusting the coverage of the first memory region in memory space, the virtual machine first obtains the memory reclamation method applicable to the object to be reclaimed. Secondly, based on the memory reclamation method corresponding to the object, it adjusts the coverage of the first memory region corresponding to that object in memory space.
[0141] The memory reclamation method applicable to the objects to be reclaimed can be pre-set, or it can be determined by the virtual machine in combination with the memory space layout information, the running speed of the first program, and the number of non-reclaimed objects.
[0142] In some examples, memory space layout information may include the size of the memory space and the layout of free memory regions within it. The layout of free memory regions can be either contiguous or distributed. A contiguous layout means that multiple free memory regions occupy a contiguous memory space, and there is no memory fragmentation. A distributed layout means that multiple free memory regions occupy a dispersed memory space, and memory fragmentation exists.
[0143] During the memory reclamation phase, when an object is to be reclaimed, in order to alleviate memory space occupancy, the virtual machine needs to reclaim the object and the memory space occupied by its corresponding first memory region. At this time, the virtual machine needs to adjust the coverage of the first memory region corresponding to the object, based on the adopted memory reclamation method and its corresponding range adjustment strategy. Specifically, for objects to be reclaimed, the virtual machine can use at least the first method and the second method when reclaiming the memory space occupied by the object.
[0144] If the virtual machine uses the first method for memory reclamation, in some examples, when the virtual machine adjusts the coverage of the first memory region in the memory space according to the memory reclamation method corresponding to the object, the virtual machine further expands the coverage of the first memory region in the memory space based on the "original" coverage of the first memory region.
[0145] The first method is either the mark-and-sweep method or the copy method.
[0146] In some examples, after the virtual machine adjusts the first memory region according to the memory reclamation method corresponding to the object, the adjusted first memory region overwrites the memory region occupied by the object corresponding to the first memory region, the original first memory region, and the memory region occupied by the region identifier, such as... Figure 10 As shown in (a).
[0147] The region identifier is set by the virtual machine for the first memory region during the memory allocation phase. The region identifier represents the original size of the first memory region. If the virtual machine does not set a region identifier to represent the size of the first memory region during the memory allocation phase, the adjusted first memory region will not include the memory region occupied by the region identifier. Figure 10 As shown in (b).
[0148] In some implementations, after the virtual machine expands the coverage of the first memory region in the memory space to obtain the adjusted first memory region, the virtual machine releases the adjusted first memory region at the memory release time to avoid the problem of memory leakage caused by long-term occupation of memory space when the first memory region cannot be accessed.
[0149] If the virtual machine adopts the second method for memory reclamation, in some examples, when the virtual machine adjusts the coverage of the first memory region in the memory space according to the memory reclamation method corresponding to the object to be reclaimed, the virtual machine releases the memory space occupied by the object and the first memory region of the object by clearing the object and the first memory region of the object.
[0150] The second method is a tag compression method.
[0151] If the object of the first program is in the memory reclamation phase, in other implementations, if the object is not a reclamation object, the storage location of the object and the object's first memory region in the memory space is adjusted to release the memory space occupied by the object and the object's first memory region.
[0152] Adjusting the storage location of an object and its first memory region in memory space can be achieved by changing the memory block occupied by the object and its corresponding first memory region in memory space, or by maintaining the memory block occupied by the object and its corresponding first memory region in memory space.
[0153] In some examples, when the virtual machine adjusts the coverage of the first memory region in memory space, it first obtains the memory reclamation method applicable to the non-reclaimed object. Secondly, based on the memory reclamation method corresponding to the non-reclaimed object, the virtual machine adjusts the storage location of the object and its corresponding first memory region in memory space to preserve the memory space occupied by the object and its corresponding first memory region.
[0154] In this way, for some non-reclaimed objects that are still in a "referenced state", the memory space they occupy will not be blindly released during the memory reclamation phase, so that they remain in an accessible state. This avoids the operating system and / or the first program from encountering problems that prevent them from running due to the inability to access the object, thereby reducing the probability of the first program encountering memory access exceptions when running in the operating system.
[0155] The memory reclamation method for the object to be reclaimed and the memory reclamation method for the object not to be reclaimed can be the same method or different methods.
[0156] During the memory reclamation phase, when an object is not eligible for reclamation, in order to maintain the memory space occupied by the non-reclaimed object while avoiding memory fragmentation, the virtual machine needs to adjust the storage location of the object and its corresponding first memory region in memory. At this time, the virtual machine needs to combine the adopted memory reclamation method with the corresponding storage location adjustment strategy to adjust the storage location of the object and its corresponding first memory region in memory. For non-reclaimed objects, the virtual machine can use at least the third and fourth methods when adjusting the memory space occupied by the non-reclaimed object and its first memory region.
[0157] If the virtual machine uses a third-party memory reclamation method, in some examples, while the virtual machine adjusts the storage location of the object and its first memory region in memory according to the memory reclamation method corresponding to the non-reclaimed object, the virtual machine maintains the storage location of the object and its first memory region in memory. In other words, when the virtual machine uses a third-party method to reclaim memory, for non-reclaimed objects, the virtual machine will not change the storage location of the object and its corresponding first memory region in memory.
[0158] The third method is the mark-and-sweep method.
[0159] In this way, when there are many non-reclaimed objects and few objects to be reclaimed, the virtual machine can quickly complete the reclamation of objects to be reclaimed because there is no need to adjust the non-reclaimed objects during the memory reclamation process (e.g., adjust the storage location of the non-reclaimed objects).
[0160] If the virtual machine uses the fourth method for memory reclamation, in some examples, when the virtual machine adjusts the storage location of the object and the object's first memory region in the memory space according to the memory reclamation method corresponding to the non-reclaimed object, the virtual machine moves the object and the object's corresponding first memory region in the memory space by adjusting the memory block occupied by the object in the memory space, thereby realizing the adjustment of the storage location of the object and the object's corresponding first memory region in the memory space.
[0161] When moving the storage location of the first memory region, at least two moving methods can be used: First, release the "original" first memory region and then reset the first memory region for the moved object to adjust the storage location of the first memory region; Second, follow the object in the memory space and adjust the memory block occupied by the first memory region in the memory space.
[0162] The fourth method is either the copying method or the tag compression method.
[0163] During the memory reclamation phase, the purpose of adjusting the storage location of non-reclaimed objects in memory space is to obtain a contiguous free memory area to the greatest extent possible by standardizing the storage location between occupied memory areas and free memory areas in memory space. However, the virtual machine adopts different adjustment schemes when adjusting the storage location of non-reclaimed objects, depending on the memory reclamation method.
[0164] In some implementations, when the virtual machine adopts a copying method as the fourth method, the memory space can be divided into a first subspace and a second subspace. Taking the storage of an object and its corresponding first memory region in the first subspace as an example, when the virtual machine moves an object and its corresponding first memory region, firstly, the virtual machine copies multiple objects together to the second subspace, and releases the first subspace occupied by each object and its corresponding first memory region by clearing each object in the first subspace. Secondly, for each object that has been copied to the second subspace, a new first memory region is set for that object in the second subspace, thereby adjusting the storage location of the object and its first memory region in the memory space.
[0165] Furthermore, after adjusting the storage location of the object, not only can a contiguous free memory area be obtained to the greatest extent, but the first memory area can also be used to detect whether there is a memory access exception in the first program.
[0166] Each object and its corresponding first memory region occupy a contiguous second subspace.
[0167] In some examples, the first subspace and the second subspace can share the memory space equally; or, the first subspace and the second subspace can divide the memory space according to a preset ratio, for example, the first subspace occupies 1 / 3 of the memory space and the second subspace occupies 2 / 3 of the memory space.
[0168] In other implementations, when the fourth method adopted by the virtual machine is the mark-and-compress method, when the virtual machine moves an object and its corresponding first memory region, firstly, the virtual machine centrally stores multiple objects in the memory space and releases the original memory space occupied by the first memory region of each object by clearing the first memory region of each object in the memory space. Secondly, for each centrally stored object, a new first memory region is set for that object in the memory space, thereby adjusting the storage location of the first memory regions corresponding to non-reclaimed objects and non-reclaimed objects in the memory space.
[0169] Furthermore, after adjusting the storage location of the object, not only can a contiguous free memory area be obtained to the greatest extent, but the first memory area can also be used to detect whether there is a memory access exception in the first program.
[0170] The following combination Figure 11 This section describes the process by which the virtual machine (JVM) reclaims objects and the memory space occupied by the object's first memory region using the mark-and-sweep method. This process involves at least two phases: a marking phase and a sweeping phase. In the marking phase, the JVM searches the object's reference state and marks it as either a pending reclaiming object or a non-reclaiming object. In the sweeping phase, if the object is a pending reclaiming object, its memory region is expanded to cover the entire first memory region, ensuring that all memory regions involved in the object are included in the first memory region. If the object is not a non-reclaiming object, it and the memory space occupied by its first memory region are preserved without further processing.
[0171] The following combination Figure 12 This section describes the process by which the virtual machine (JVM) reclaims objects and the memory space occupied by their first memory region using the mark-and-compact method. This process involves at least two phases: a marking phase and a compaction phase. In the marking phase, the JVM searches the object's reference state and marks it as either a target object to be reclaimed or a non-reclaimable object. In the compaction phase, if the object is a target object to be reclaimed, it and the memory space occupied by its first memory region are released. If the object is not a target object to be reclaimed, it is moved, and a new first memory region is generated for the moved object.
[0172] The following combination Figure 13 This section describes the process by which the virtual machine (JVM) reclaims objects and the memory space occupied by the object's first memory region using the copying method. This process involves at least two phases: a marking phase and a copying phase. In the marking phase, the JVM searches the object's reference state and marks it as either a pending reclaiming object or a non-reclaiming object. In the copying phase, if the object is a pending reclaiming object, its memory region is expanded to cover the entire first memory region, ensuring that all memory areas involved in the object are included in the first memory region. If the object is not a non-reclaiming object, it is copied from the first subspace to the second subspace, and a new first memory region is generated for the copied object.
[0173] In some implementations, during the operating system usage phase, the memory usage of the electronic device can be checked and compared with the memory data file (hprof file) to monitor whether the virtual machine detects memory access anomalies in the first program by executing the above-mentioned memory detection method during the execution of the first program.
[0174] The operating system can use the "dumpsys" command to view the memory usage of electronic devices and the "dumpheap" command to capture memory data files (hprof files).
[0175] In some solutions, multiple embodiments of this application can be combined, and the combined solution can be implemented. Optionally, some operations in the processes of each method embodiment may be combined, and / or the order of some operations may be changed. Furthermore, the execution order between the steps of each process is merely exemplary and does not constitute a limitation on the execution order between steps; other execution orders are also possible. It is not intended to indicate that the execution order is the only possible order in which these operations can be performed. Those skilled in the art will conceive of various ways to reorder the operations described in the embodiments of this application. In addition, it should be noted that the process details involved in one embodiment of this application are also applicable to other embodiments in a similar manner, or different embodiments may be combined.
[0176] Furthermore, some steps in the method embodiments can be equivalently replaced with other possible steps. Alternatively, some steps in the method embodiments may be optional and can be deleted in certain use cases. Or, other possible steps may be added to the method embodiments.
[0177] Furthermore, the various method embodiments can be implemented individually or in combination.
[0178] It is understood that, in order to achieve the above functions, the aforementioned electronic device includes hardware and / or software modules corresponding to perform each function. Based on the algorithmic steps of the various examples described in conjunction with the embodiments disclosed herein, this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in conjunction with the embodiments, but such implementation should not be considered beyond the scope of this application.
[0179] This application embodiment can divide the electronic device into functional modules according to the above method example. For example, each function can be divided into its own functional module, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware. It should be noted that the module division in this embodiment is illustrative and only represents one logical functional division. In actual implementation, there may be other division methods.
[0180] This application also provides an electronic device, such as... Figure 14 As shown, the electronic device may include one or more processors 1401, memory 1402 and communication interface 1403.
[0181] The memory 1402, communication interface 1403, and processor 1401 are coupled together. For example, the memory 1402, communication interface 1403, and processor 1401 can be coupled together via bus 1404.
[0182] The communication interface 1403 is used for data transmission with other devices. The memory 1402 stores computer program code. The computer program code includes computer instructions, which, when executed by the processor 1401, cause the electronic device to perform the login method described in this embodiment.
[0183] The processor 1401 may be a processor or controller, such as a central processing unit (CPU), a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It can implement or execute the various exemplary logic blocks, modules, and circuits described in connection with this disclosure. The processor may also be a combination that implements computational functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.
[0184] The bus 1404 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The bus 1404 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 14 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0185] This application also provides a computer-readable storage medium storing computer program code. When the processor executes the computer program code, the electronic device executes the relevant method steps in the above method embodiments.
[0186] The electronic devices and computer storage media provided in this application are used to execute the corresponding methods provided above. Therefore, the beneficial effects they can achieve can be referred to the beneficial effects of the corresponding methods provided above, and will not be repeated here.
[0187] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0188] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0189] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0190] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0191] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiments of this application, in essence, or the part that contributes, or all or part of the technical solution, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0192] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method for detecting memory anomalies, characterized in that, The method includes: Obtain at least one object corresponding to the first program; the object is used to store the program data of the first program; at least one of the objects is stored in memory space. For each of the objects, a first memory region is set for the object; the first memory region occupies contiguous memory space with the object; the first memory region is in a restricted access state; If the first program requests access to the first memory region during its execution, it is determined that the memory access of the first program is abnormal.
2. The method according to claim 1, characterized in that, After determining that the memory access error of the first program is abnormal, the method further includes: Obtain the stack information of the first program; the stack information is used to indicate the location of the memory access exception in the first program.
3. The method according to claim 1, characterized in that, Setting the first memory region of the object includes: During the memory allocation phase, a first memory region is allocated for the object; or, During the memory reclamation phase, adjust the coverage area of the first memory region in the memory space; or, During the memory reclamation phase, the storage location of the object and its first memory region in the memory space is adjusted.
4. The method according to claim 3, characterized in that, Allocating a first memory region for the object includes: Allocate a first memory region and a region identifier for the object; the region identifier is used to indicate the size of the memory space occupied by the first memory region; the object, the first memory region, and the region identifier occupy contiguous memory space.
5. The method according to claim 3, characterized in that, Adjusting the coverage area of the first memory region in the memory space includes: If the object is a reclaimable object, the coverage of the first memory region in the memory space is adjusted according to the memory reclamation method corresponding to the reclaimable object.
6. The method according to claim 3, characterized in that, Adjusting the storage location of the object and its first memory region in memory space includes: If the object is not a garbage collected object, the storage location of the object and its first memory region in the memory space is adjusted according to the memory reclamation method corresponding to the non-garbage collected object.
7. The method according to claim 5, characterized in that, The step of adjusting the coverage of the first memory region in the memory space according to the memory reclamation method corresponding to the object to be reclaimed includes: When the memory reclamation method is the first method, the coverage area of the first memory region in the memory space is expanded; When the memory reclamation method is the second method, the memory space occupied by the object and the first memory region is released.
8. The method according to claim 7, characterized in that, After expanding the coverage of the first memory region in the memory space, the resulting adjusted first memory region covers the memory region occupied by the object, the original first memory region, and the memory region occupied by the region identifier.
9. The method according to claim 6, characterized in that, The step of adjusting the storage location of the object and its first memory region in memory space according to the memory reclamation method corresponding to the non-reclaimed object includes: When the memory reclamation method is a third method, the storage location of the object and the object's first memory region in the memory space is maintained; When the memory reclamation method is the fourth method, the object and the object's first memory region are moved to adjust the storage location of the object and the object's first memory region in the memory space.
10. The method according to claim 9, characterized in that, The fourth method is a copying method, and the memory space includes a first subspace and a second subspace. The object and the first memory region of the object are stored in the first subspace. Moving the object and its first memory region includes: The multiple objects are copied together into the second subspace, and the first subspace occupied by each object and the first memory region of each object is released; For each object stored in the second subspace, a first memory region is set for the object; the object and the first memory region occupy a contiguous second subspace.
11. The method according to claim 9, characterized in that, The fourth method is a mark-compression method, which moves the object and the object's first memory region, including: Multiple objects are stored together in the memory space, and the original memory space occupied by the first memory region of each object is released; For each object after centralized storage, a first memory region is set up for that object.
12. The method according to claim 8, characterized in that, After expanding the coverage area of the first memory region in the memory space to obtain the adjusted first memory region, the method further includes: At the time of memory release, the adjusted first memory region is released.
13. The method according to claim 1, characterized in that, The first program requests access to the first memory region, including: The first program requests to write data to the first memory region; or, The first program requests to read data from the first memory region.
14. The method according to claim 7, characterized in that, The first method is either a mark-clearing method or a copying method; the second method is a mark-compression method.
15. The method according to claim 9, characterized in that, The third method is a mark-clearing method; the fourth method is either a copying method or a mark-compression method.
16. An electronic device, characterized in that, The device includes a memory and one or more processors; the memory is coupled to the processors; the memory stores computer program code, the computer program code including computer instructions, which, when executed by the processor, cause the electronic device to perform the memory anomaly detection method as described in any one of claims 1-15.
17. A computer-readable storage medium, characterized in that, The method includes computer instructions that, when executed on an electronic device, cause the electronic device to perform the memory anomaly detection method as described in any one of claims 1-15.
18. A computer program product, characterized in that, When the computer program product is run on an electronic device, the electronic device performs the memory anomaly detection method as described in any one of claims 1-15.