Loop detection method and apparatus
By analyzing the intermediate representation of the application and the dependencies of loop body variables, the problems of false positives and resource overhead in dead loop detection are solved, and more efficient dead loop detection and CPU utilization are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HUAWEI TECH CO LTD
- Filing Date
- 2020-10-31
- Publication Date
- 2026-06-09
AI Technical Summary
In existing technologies, detecting infinite loops based on CPU usage and the execution status of scheduled tasks is prone to misjudgment and incurs high resource consumption, affecting CPU operating efficiency.
By determining the intermediate representation of the application, including linear intermediate representation and graph intermediate representation, the loop body is obtained, and the variable dependencies and static single assignment representation in the loop body are analyzed to determine whether there is a risk of infinite loop and avoid dependence on CPU load status.
It improves the accuracy and efficiency of dead loop detection, reduces continuous monitoring of CPU resources, and enhances CPU operating efficiency.
Smart Images

Figure CN114443455B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing, and in particular to a method and apparatus for detecting infinite loops. Background Technology
[0002] Infinite loops are a common program defect. Currently, the existence of infinite loops in an application can be detected by judging the termination conditions during application execution. Specifically, infinite loops can be detected based on central processing unit (CPU) usage or the execution status of scheduled tasks. For example, in a CPU usage-based approach, an infinite loop is determined by monitoring the CPU usage of the application's corresponding process during runtime. If the process's CPU usage exceeds a usage threshold for a duration greater than or equal to the threshold, an infinite loop is identified. As another example, in a scheduled task-based approach, a low-priority thread can be started on the device running the application to execute a specific task at regular intervals. This thread acquires the CPU resources required to execute the specific task; if the thread fails to acquire CPU resources and does not execute the specific task within a certain time, an infinite loop is identified.
[0003] The aforementioned solutions based on CPU usage and those based on the execution status of scheduled tasks both rely on CPU load to determine if an infinite loop has occurred. However, during the execution of computationally intensive tasks, the CPU is typically under continuous high load. Therefore, using CPU load to detect infinite loops may lead to false positives, resulting in low accuracy in identifying infinite loops. Furthermore, solutions based on CPU usage or the execution status of scheduled tasks require continuous monitoring of the CPU or specific task status, incurring additional resource overhead and impacting CPU efficiency. Summary of the Invention
[0004] This application provides a method and apparatus for detecting infinite loops, which can solve the problems of false positives and high resource consumption in detecting infinite loops, thereby improving the detection accuracy of infinite loops and the operating efficiency of the CPU.
[0005] To achieve the above objectives, this application adopts the following technical solution:
[0006] Firstly, a method for detecting infinite loops is provided. This method includes: determining an intermediate representation of an application. The intermediate representation is determined based on the application's source code and / or installation package, and includes linear intermediate representations and graph intermediate representations. A loop body is obtained based on the intermediate representation, and it is determined whether the loop body poses a risk of an infinite loop.
[0007] Based on the dead loop detection method described in the first aspect, the loop body is obtained according to the intermediate representation of the application, and it is determined whether there is a risk of dead loop in the loop body. For example, the risk of dead loop in the loop body can be determined according to the source code of the application or the application installation package. The influence of the CPU load state on the dead loop detection result can be avoided, thereby improving the detection accuracy of dead loop.
[0008] Furthermore, the solution provided in this application avoids continuously monitoring CPU usage or the execution status of scheduled tasks during application operation when detecting infinite loops, which can reduce resource overhead and thus improve CPU efficiency.
[0009] In one possible design, the intermediate representation of the graph may include a method call graph. This graph represents the call relationships between methods. Obtaining the loop body based on this intermediate representation could include: performing a depth-first search on each node in the method call graph to obtain strongly connected components, and then determining the loop body based on these components. Each node represents a method. This independent search on each method allows for concurrent processing and improves the efficiency of detecting infinite loops.
[0010] In one possible design scheme, determining the risk of an infinite loop within the loop body can include: identifying the dependencies between variables within the loop body. If a first condition is met, then the loop body is determined to be at risk of an infinite loop. The first condition is determined based on the dependencies. The first condition may include: the loop variable within the loop body has not been assigned a value. Thus, by analyzing the dependencies between variables to determine the variables the loop variable depends on, the efficiency of infinite loop detection can be improved.
[0011] In another possible design, the linear intermediate representation may include a static single-assignment representation.
[0012] Determining the risk of an infinite loop within a loop body can include: determining the static single-assignment representation of the loop body. If a second condition is met, then the risk of an infinite loop is determined. This second condition can include: all variables in the initial loop condition of the static single-assignment representation have been assigned values. Thus, directly determining the risk of an infinite loop based on the static single-assignment representation of the loop body reduces computational load and improves the efficiency of infinite loop detection. Furthermore, analyzing the variable assignments in the initial loop condition can further improve detection accuracy.
[0013] In another possible design, determining the risk of an infinite loop in the loop body can include: determining the variable of the loop body. The variable is the difference between the loop conditions during two consecutive executions of the loop body. During the execution of the loop body, the unknown loop variable is represented by a first variable. If a third condition is met, then the risk of an infinite loop in the loop body is determined. The third condition is determined based on the first variable, the loop condition, the initial condition, and the variable. Thus, detecting infinite loops based on the third condition improves the efficiency of infinite loop detection.
[0014] Optionally, the third condition may include one of the following: there exists a value for the first variable such that the initial condition is satisfied, and the sign of the change is the same as the sign of the loop condition. The sign includes one of the following: positive, non-negative, 0, non-positive, or negative. Alternatively, there exists a value for the first variable such that when the sign of the loop condition is not 0, the initial condition is satisfied, the sign of the initial condition is positive or negative, and the change is 0. Alternatively, there exists a value for the first variable such that the sign of the loop condition includes both positive and negative, the sign of the initial condition is not 0, and the initial condition is a non-integer multiple of the change. Thus, when the value of the first variable is determined, judging the relationship between the initial condition, the loop condition, and the change can identify infinite loops, improving the efficiency of infinite loop detection.
[0015] Secondly, a dead loop detection device is provided, comprising: a determining module and an acquiring module. The determining module is used to determine an intermediate representation of an application. The intermediate representation is determined based on the application's source code and / or installation package, and includes linear intermediate representations and graph intermediate representations. The acquiring module is used to acquire the loop body based on the intermediate representation. The determining module is also used to determine whether the loop body poses a risk of dead looping.
[0016] In one possible design, the graph in the middle represents a method call graph. This graph represents the call relationships between methods. An acquisition module performs a depth-first search on each node in the method call graph to obtain strongly connected components. Each node represents a method. This acquisition module also determines the loop body based on the strongly connected components.
[0017] In one possible design, the determining module is also used to determine the dependencies between variables in the loop body.
[0018] The determination module is also used to determine if the first condition is met and to determine if there is a risk of an infinite loop occurring within the loop body. The first condition is determined based on dependencies. The first condition includes: the loop variable within the loop body has not been assigned a value within the loop body.
[0019] In another possible design, the linear intermediate representation may include a static single-assignment representation. The determining module is also used to determine the static single-assignment representation of the loop body. The determining module is also used to determine that the second condition is met and to determine that the loop body has a risk of entering an infinite loop. The second condition includes: all variables included in the initial loop condition of the static single-assignment representation have been assigned values.
[0020] In another possible design, the determining module is also used to determine the change in the loop body. The change is the difference in the loop condition between two consecutive executions of the loop body. During the execution of the loop body, the unknown loop variable is represented by the first variable. The determining module is also used to determine if the third condition is met and to determine if the loop body has the risk of entering an infinite loop. The third condition is determined based on the first variable, the loop condition, the initial condition, and the change.
[0021] Optionally, the third condition may include one of the following: there exists a value for the first variable such that the initial condition is satisfied, and the sign of the change is the same as the sign of the loop condition. The sign includes one of the following: positive, non-negative, 0, non-positive, or negative. Alternatively, there exists a value for the first variable such that the initial condition is satisfied when the sign of the loop condition is not 0, the initial condition has a positive or negative sign, and the change is 0. Alternatively, there exists a value for the first variable such that the sign of the loop condition includes both positive and negative, the sign of the initial condition is not 0, and the initial condition is a non-integer multiple of the change.
[0022] It should be noted that the determination module and the acquisition module can also be integrated into a single module, such as the processing module. The processing module is used to implement the operations of the aforementioned functional modules.
[0023] Optionally, the dead loop detection device described in the second aspect may further include a storage module that stores a program or instructions. When the processing module executes the program or instructions, the dead loop detection device can perform the dead loop detection method described in the first aspect.
[0024] Optionally, the dead loop detection device described in the second aspect may further include a transceiver module. The transceiver module is used to implement the transceiver function of the dead loop detection device described in the second aspect.
[0025] Furthermore, the transceiver module may include a receiving module and a sending module. The receiving module implements the receiving function of the dead loop detection device described in the second aspect, and the sending module implements the sending function of the dead loop detection device described in the second aspect.
[0026] It should be noted that the dead loop detection device described in the second aspect can be a terminal device or a network device, or it can be a chip (system) or other component or assembly that can be set in a terminal device or a network device, or it can be a device that includes a terminal device or a network device. This application does not limit it in this regard.
[0027] Furthermore, the technical effects of the dead loop detection device described in the second aspect can be referred to the technical effects of the dead loop detection method described in the first aspect, and will not be repeated here.
[0028] Thirdly, a dead loop detection device is provided. This dead loop detection device is used to execute the dead loop detection method described in the implementation of the first aspect.
[0029] In this application, the dead loop detection device described in the third aspect can be the terminal device or network device in the first aspect, or a chip (system) or other component or assembly disposed in the terminal device or network device, or a device containing the terminal device or network device.
[0030] It should be understood that the dead loop detection device described in the third aspect includes modules, units, or means that implement the dead loop detection method described in the first aspect. These modules, units, or means can be implemented in hardware, software, or by hardware executing corresponding software. The hardware or software includes one or more modules or units for performing the functions involved in the dead loop detection method described above.
[0031] Furthermore, the technical effects of the dead loop detection device described in the third aspect can be referred to the technical effects of the dead loop detection method described in the first aspect, and will not be repeated here.
[0032] Fourthly, a dead loop detection device is provided. The dead loop detection device includes a processor configured to execute the dead loop detection method described in the implementation of the first aspect.
[0033] In one possible design, the dead loop detection device described in the fourth aspect may further include a transceiver. This transceiver may be a transceiver circuit or an interface circuit. The transceiver can be used for communication between the dead loop detection device described in the fourth aspect and other devices.
[0034] In one possible design, the infinite loop detection device described in the fourth aspect may further include a memory. This memory may be integrated with the processor or disposed separately. The memory may be used to store the computer program and / or data involved in the infinite loop detection method described in the first aspect.
[0035] In this application, the dead loop detection device described in the fourth aspect can be the terminal device or network device in the first aspect, or a chip (system) or other component or assembly disposed in the terminal device or network device, or a device containing the terminal device or network device.
[0036] Furthermore, the technical effects of the dead loop detection device described in the fourth aspect can be referred to the technical effects of the dead loop detection method described in the implementation of the first aspect, and will not be repeated here.
[0037] Fifthly, a dead loop detection device is provided. The dead loop detection device includes: a processor coupled to a memory, the processor executing a computer program stored in the memory, such that the dead loop detection device performs the dead loop detection method described in the implementation of the first aspect.
[0038] In one possible design, the dead loop detection device described in the fifth aspect may further include a transceiver. This transceiver may be a transceiver circuit or an interface circuit. The transceiver can be used for communication between the dead loop detection device described in the fifth aspect and other devices.
[0039] In this application, the dead loop detection device described in the fifth aspect can be the terminal device or network device in the first aspect, or a chip (system) or other component or assembly disposed in the terminal device or network device, or a device containing the terminal device or network device.
[0040] Furthermore, the technical effects of the dead loop detection device described in the fifth aspect can be referred to the technical effects of the dead loop detection method described in the implementation of the first aspect, and will not be repeated here.
[0041] In a sixth aspect, a dead loop detection device is provided, comprising: a processor and a memory; the memory is used to store a computer program, which, when executed by the processor, causes the dead loop detection device to perform the dead loop detection method described in the implementation of the first aspect.
[0042] In one possible design, the dead loop detection device described in the sixth aspect may further include a transceiver. This transceiver may be a transceiver circuit or an interface circuit. The transceiver can be used for communication between the dead loop detection device described in the sixth aspect and other devices.
[0043] In this application, the dead loop detection device described in the sixth aspect can be the terminal device or network device in the first aspect, or a chip (system) or other component or assembly disposed in the terminal device or network device, or a device containing the terminal device or network device.
[0044] Furthermore, the technical effects of the dead loop detection device described in the sixth aspect can be referred to the technical effects of the dead loop detection method described in the implementation of the first aspect, and will not be repeated here.
[0045] In a seventh aspect, a dead loop detection device is provided, comprising: a processor; the processor is configured to be coupled to a memory, and after reading a computer program from the memory, execute the dead loop detection method as described in the implementation of the first aspect according to the computer program.
[0046] In one possible design, the dead loop detection device described in the seventh aspect may further include a transceiver. This transceiver may be a transceiver circuit or an interface circuit. The transceiver can be used for communication between the dead loop detection device described in the seventh aspect and other devices.
[0047] In this application, the dead loop detection device described in the seventh aspect can be the terminal device or network device of the first aspect, or a chip (system) or other component or assembly disposed in the terminal device or network device, or a device containing the terminal device or network device.
[0048] Furthermore, the technical effects of the dead loop detection device described in the seventh aspect can be referred to the technical effects of the dead loop detection method described in the implementation of the first aspect, and will not be repeated here.
[0049] Furthermore, the technical effects of the dead loop detection device described in the third to seventh aspects above can be referred to the technical effects of the dead loop detection method described in the first aspect above, and will not be repeated here.
[0050] Eighthly, a processor is provided. The processor is used to execute the infinite loop detection method described in the implementation of the first aspect.
[0051] Ninthly, a dead loop detection system is provided. The dead loop detection system includes one or more terminal devices and one or more network devices.
[0052] In a tenth aspect, a computer-readable storage medium is provided, comprising: a computer program or instructions; when the computer program or instructions are executed on a computer, the computer causes the computer to perform the dead loop detection method described in the implementation of the first aspect.
[0053] Eleventhly, a computer program product is provided, including a computer program or instructions that, when run on a computer, cause the computer to execute the infinite loop detection method described in the implementation of the first aspect. Attached Figure Description
[0054] Figure 1 This is a schematic diagram of the architecture of a terminal device provided in an embodiment of this application;
[0055] Figure 2 A flowchart illustrating the dead loop detection method provided in this application embodiment;
[0056] Figure 3 This is a schematic diagram of the structure of the search tree provided in an embodiment of this application;
[0057] Figure 4 This is a schematic diagram of the control flow graph provided in an embodiment of this application;
[0058] Figure 5 A schematic diagram of the loop path provided in the embodiments of this application. Figure 1 ;
[0059] Figure 6 A schematic diagram of the loop path provided in the embodiments of this application. Figure 2 ;
[0060] Figure 7 A schematic diagram of the loop path provided in the embodiments of this application. Figure 3 ;
[0061] Figure 8 Schematic diagram of the dead loop detection device provided in the embodiments of this application Figure 1 ;
[0062] Figure 9 Schematic diagram of the dead loop detection device provided in the embodiments of this application Figure 2 . Detailed Implementation
[0063] The technical terms used in the embodiments of this application will be introduced below.
[0064] 1. Strongly Connected Component: In a directed graph, the strongly connected component is the part where two vertices are strongly connected. Strong connectivity between two vertices means that in a directed graph G, there is a directed path from vertices vi and vj (vi>vj) between them, and simultaneously, there is a directed path from vj to vi.
[0065] 2. Natural Loop: A natural loop has a head node as the sole entry point, which "dominates" every node in the loop; or, it has a loop with a reverse edge pointing to the head node. Examples include loops constructed using for, if-else, while, continue, break, or goto statements.
[0066] 3. Static Single Assignment Form (SSA): This is an intermediate representation (IR) in which each variable is assigned a value only once.
[0067] 4. Depth-first search (DFS): Depth-first search is a type of graph algorithm. Its process involves exploring every possible branch path until it can be explored as deeply as possible, and each node can only be visited once.
[0068] 5. Symbolic execution: This means using symbolic values instead of actual values for execution.
[0069] The technical solutions in this application will now be described with reference to the accompanying drawings.
[0070] The technical solutions of this application embodiment can be applied to electronic devices, such as network devices or terminal devices, which have data processing functions.
[0071] This application will focus on various aspects, embodiments, or features related to terminal devices that may include multiple components, modules, etc. It should be understood and appreciated that individual systems may include additional components, modules, etc., and / or may not include all the components, modules, etc. discussed in conjunction with the accompanying drawings. Furthermore, combinations of these approaches are also possible.
[0072] Furthermore, in the embodiments of this application, words such as "exemplarily" and "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design that is described as an "example" in this application should not be construed as being better or more advantageous than other embodiments or designs. Rather, the use of the word "example" is intended to present the concept in a specific manner.
[0073] In the embodiments of this application, "of", "corresponding (relevant)" and "corresponding" can sometimes be used interchangeably. It should be noted that when their differences are not emphasized, their meanings are consistent.
[0074] In the embodiments of this application, sometimes the subscript such as W1 may be mistakenly written as a non-subscript form such as W1. When the difference is not emphasized, the meaning they express is the same.
[0075] The electronic devices and 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 in the embodiments of this application. As those skilled in the art will know, with the evolution of network architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0076] To facilitate understanding of the embodiments of this application, let's first take... Figure 1 The terminal device 100 shown in the figure is used as an example to describe in detail the device applicable to the embodiments of this application.
[0077] Figure 1 This is a schematic diagram of the structure of a terminal device 100 to which the dead loop detection method provided in this application embodiment applies. For example... Figure 1 As shown, the terminal device 100 includes a processor 110, internal memory 121, external memory interface 122, antenna A, mobile communication module 131, antenna B, wireless communication module 132, audio module 140, speaker 140A, receiver 140B, microphone 140C, headphone jack 140D, display screen 151, subscriber identification module (SIM) card interface 152, camera 153, buttons 154, sensor module 160, universal serial bus (USB) interface 170, charging management module 180, power management module 181, and battery 182. In other embodiments, the terminal device 100 may also include a motor, indicator, etc.
[0078] The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (AP), a modem, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural network processing unit (NPU). It should be noted that different processing units can be independent devices or integrated into one or more independent processors, or integrated into the same device with other modules in the terminal device. Taking the modem as an example, the modem can be a processing unit independent of the processor 110, or it can be integrated into the same device with other processing units (e.g., AP, ISP, GPU, etc.), or some or all of its functions can be integrated into the same device with the mobile communication module 131. Similarly, taking the controller as an example, the controller can be a processing unit independent of the processor 110, or it can be integrated into the same device with other processing units (e.g., video codec, digital signal processor, etc.), or some or all of its functions can be integrated into the same device with the mobile communication module 131.
[0079] Internal memory 121 can be used to store data and / or at least one computer program, which includes instructions. Specifically, internal memory 121 may include a program storage area and a data storage area. The program storage area may store at least one computer program. The computer program may include an application program (such as a gallery, contacts, etc.), an operating system (such as Android or iOS), or other programs; for example, the computer program may include a program for performing static analysis. The data storage area may store at least one of the following: data created during the use of the terminal device, data received from other devices (such as other terminal devices, network devices, servers, external storage, etc.), or data pre-stored before leaving the factory. For example, the data stored in internal memory 121 may be at least one of the following: an application program, application source code, images, files, or identifiers.
[0080] In some embodiments, internal memory 121 may include high-speed random access memory and / or non-volatile memory. For example, internal memory 121 may include one or more disk storage devices, flash memory devices, or universal flash storage (UFS), etc.
[0081] The processor 110 can invoke one or more computer programs and / or data stored in the internal memory 121, thereby enabling the terminal device to perform one or more functions to meet user needs. For example, the processor 110 can invoke instructions and data stored in the internal memory 121 to enable the terminal device to execute the infinite loop detection method provided in this embodiment.
[0082] The external storage interface 122 can be used to connect an external storage card (e.g., a Micro SD card) to expand the storage capacity of the terminal device. The external storage interface 122 can also be used to connect hard drives, optical discs, USB flash drives, etc. The external storage card communicates with the processor 110 through the external storage interface 122 to implement data storage functions. For example, images, music, videos, application installation packages, application source code, and other files can be stored on the external storage card. In some embodiments, the processor 110 can also have a cache area to store instructions and / or data that the processor 110 needs to use repeatedly. If the processor 110 needs to use the instruction or data again, it can directly retrieve it from the cache area, thereby helping to avoid repeated accesses, reduce the processor 110's waiting time, and thus help improve system efficiency. For example, the cache area can be implemented using a high-speed cache memory.
[0083] Optionally, the processor 110 may retrieve the data to be processed or the instructions to be executed from the internal memory 121 or from the external memory interface 122. After retrieving the data to be processed or the instructions to be executed, the processor 110 may first store the data to be processed or the instructions to be executed in the cache.
[0084] Antenna A and antenna B are used to transmit and receive electromagnetic wave signals. Each antenna in the terminal device can be used to cover one or more communication frequency bands. Different antennas can also be reused to improve antenna utilization. For example, antenna A can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antennas can be used in conjunction with a tuning switch.
[0085] The mobile communication module 131 can be used to enable communication between the terminal device and network devices based on the mobile communication technologies (e.g., 2G, 3G, 4G, or 5G) supported by the terminal device. For example, the mobile communication technologies supported by the terminal device may include at least one of Global System for Mobile Communication (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Time Division-Synchronous Code Division Multiple Access (TD-SCDMA), Long Term Evolution (LTE), or New Radio (NR). For instance, if the terminal device supports GSM, after accessing the network through a cell provided by a base transceiver station (BTS) in the GSM communication system, communication between the terminal device and the BTS can be achieved through the mobile communication module 131, provided that the network signal strength of the accessed cell is not lower than a decision threshold, i.e., while the terminal device is in a network-hosted state. For example, the mobile communication module 131 can amplify the signal modulated by the modem and then transmit it to the network device via antenna A; alternatively, the mobile communication module 131 can receive signals transmitted by the network device via antenna A, amplify them, and then send them to the modem, whereby the modem demodulates the received signal into a low-frequency baseband signal and then performs other corresponding processing. In some embodiments, the mobile communication module 131 may include filters, switches, power amplifiers, low-noise amplifiers (LNAs), etc.
[0086] The wireless communication module 132 can provide wireless communication solutions for terminal devices, including wireless local area networks (WLANs) (such as wireless-fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), and infrared (IR) technologies. The GNSS may include at least one of the following: global positioning system (GPS), global navigation satellite system (GLONASS), BeiDou navigation satellite system (BDS), quasi-zenith satellite system (QZSS), and / or satellite-based augmentation systems (SBAS). For example, the wireless communication module 132 may be one or more devices integrating at least one communication processing module. The wireless communication module 132 can communicate with corresponding devices via antenna B according to its supported wireless communication technologies (e.g., Wi-Fi, Bluetooth, FM, or NFC).
[0087] Terminal devices can implement audio functions such as music playback and recording through audio modules 140, speakers 140A, receivers 140B, microphones 140C, headphone jacks 140D, and access points (APs).
[0088] The terminal device can implement display functions through a GPU, display screen 151, and AP, etc. Display screen 151 can be used to display images, videos, etc. Display screen 151 can include a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), a Miniled LED, a MicroLED, a Micro-OLED, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the terminal device can include one or N displays 151, where N is a positive integer greater than 1.
[0089] Button 154 may include a power button, volume buttons, etc. Button 154 can be a mechanical button, a virtual button, or a virtual option. The terminal device can receive button input and generate key signal inputs related to user settings and function control. For example, in response to selecting a virtual option indicating agreement to participate in a "user experience improvement program," the terminal device can collect or analyze information about the user's use of the terminal device to provide more personalized services and improve the user experience.
[0090] Sensor module 160 may include one or more sensors. For example, sensor module 160 may include an accelerometer 160A, a touch sensor 160B, a fingerprint sensor 160C, etc. In some embodiments, sensor module 160 may also include a pressure sensor, a gyroscope sensor, an environmental sensor, a distance sensor, a proximity sensor, a bone conduction sensor, etc.
[0091] The acceleration sensor (ACC sensor) 160A can collect the magnitude of acceleration of a terminal device in various directions (generally three axes). When the terminal device is stationary, it can detect the magnitude and direction of gravity. Furthermore, the ACC sensor 160A can also be used to identify the attitude of the terminal device, applicable to screen orientation switching, pedometers, and other applications. In some embodiments, the ACC sensor 160A can be connected to the processor 110 via a microcontroller unit (MCU), thereby helping to save power consumption of the terminal device. For example, the ACC sensor 160A can be connected to an access point (AP) or modem via the MCU. In some embodiments, the MCU can be a general-purpose smart sensor hub.
[0092] Touch sensor 160B, also known as a "touch panel," can be located on display screen 151. The touch sensor 160B and display screen 151 together form a touchscreen, also called a "touch screen." Touch sensor 160B detects touch operations applied to or near it. It transmits the detected touch operation to the AP (Application Processor) to determine the touch event type. The terminal device then provides visual output related to the touch operation through display screen 151 based on the determined touch event type. In some embodiments, touch sensor 160B may also be located on the surface of the terminal device, in a different position than display screen 151.
[0093] The 160C fingerprint sensor is used to collect fingerprints. Terminal devices can use the collected fingerprint characteristics to unlock devices, access app locks, take photos with fingerprints, and answer calls with fingerprints.
[0094] In other embodiments, the processor 110 may also include one or more interfaces. For example, the interface may be a SIM card interface 152. Another example is a USB interface 170. Yet another example is an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), or a general-purpose input / output (GPIO) interface. It is understood that the processor 110 in this embodiment can connect to different modules of the terminal device through interfaces, thereby enabling the terminal device to perform different functions, such as searching the network and taking pictures. It should be noted that this embodiment does not limit the connection method of the interfaces in the terminal device.
[0095] The SIM card interface 152 is used to connect a SIM card. The SIM card can be inserted into or removed from the SIM card interface 152 to achieve contact and separation with the terminal device. The terminal device can support one or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 152 can support Nano SIM cards, Micro SIM cards, SIM cards, etc. Multiple cards can be inserted into the same SIM card interface 152 simultaneously. The multiple cards can be of the same or different types. The SIM card interface 152 can also be compatible with different types of SIM cards. In some embodiments, the SIM card interface 152 can also be compatible with external storage cards. The terminal device uses the SIM card to perform functions such as calls and data communication. In some embodiments, the terminal device can also use an eSIM, i.e., an embedded SIM card. The eSIM card can be embedded in the terminal device and cannot be separated from the terminal device.
[0096] The USB 170 port is a USB standard compliant interface, which can be a Mini USB port, Micro USB port, USB Type-C port, etc. The USB 170 port can be used to connect a charger to charge terminal devices, and can also be used for data transfer between terminal devices and peripheral devices. It can also be used to connect headphones for audio playback. This port can also be used to connect other electronic devices, such as AR devices.
[0097] It is understood that the interface connection relationships between the modules illustrated in the embodiments of the present invention are merely illustrative and do not constitute a structural limitation on the terminal device. In other embodiments of this application, the terminal device may also employ different interface connection methods or combinations of multiple interface connection methods as described in the above embodiments.
[0098] The charging management module 180 receives charging input from the charger. The charger can be a wireless charger or a wired charger. The power management module 181 connects the battery 182, the charging management module 180, and the processor 110. The power management module 181 receives input from the battery 182 and / or the charging management module 180 to power the processor 110 and other modules. In some embodiments, the power management module 181 can also monitor parameters such as battery capacity, battery cycle count, and battery health status (leakage current, impedance).
[0099] It should be understood that Figure 1 The structure of the terminal device shown is merely an example. The terminal device of this application embodiment may have more or fewer components than those shown in the figures, may combine two or more components, or may have different component configurations. The various components shown in the figures can be implemented in hardware, software, or a combination of hardware and software, including one or more signal processing and / or application-specific integrated circuits.
[0100] It should be noted that the dead loop detection method provided in this application embodiment can be applied to... Figure 1 The terminal devices shown, such as mobile phones, tablets, and computers, can be specifically implemented by referring to the following method embodiments, which will not be repeated here.
[0101] It should be noted that the solutions in this application embodiment can also be applied to other terminal devices, and the corresponding names can be replaced by the names of the corresponding functions in other terminal devices. It should be understood that... Figure 1 This is a simplified illustration for ease of understanding only. The terminal device may also include other modules and / or other components. Figure 1 It was not drawn in the middle.
[0102] It is easy to understand that the solutions in the embodiments of this application can be applied not only to the aforementioned terminal devices, but also to network terminal devices and other electronic devices.
[0103] The following will combine Figures 2-7 The dead loop detection method provided in the embodiments of this application will be described in detail.
[0104] For example, Figure 2 A flowchart illustrating the dead loop detection method provided in this application embodiment. Figure 1 This dead loop detection method can be applied to... Figure 1The terminal device shown.
[0105] like Figure 2 As shown, the dead loop detection method includes the following steps:
[0106] S201, Determine the intermediate representation of the application.
[0107] The intermediate representation is determined based on the application's source code and / or installation package, and may include linear intermediate representation and graph intermediate representation.
[0108] For example, an application can be a computer program that performs one or more specific tasks, runs in user mode, interacts with the user, and has a visual user interface. The type of application can be an Android application; there is no limitation on the type of application. For example, source code is an uncompiled text file written according to a certain programming language specification, such as code written in assembly language or a high-level language.
[0109] For example, an installation package can be a collection of self-unzipping files that includes all the files needed to install the application. For instance, an installation package could be the files corresponding to an application downloaded from an app store, such as an Android package (APK).
[0110] For example, an intermediate representation is an equivalent internal representation of the source code that is easier to translate into the target program than the source code itself. During the compilation process, the source code is translated into one or more intermediate representations, and then the intermediate representations are translated into the target code. For example, in a compiler architecture, the source code can be translated into one or more intermediate representations such as: lexical unit sequences, abstract syntax trees, three-address code, directed acyclic graphs, etc.
[0111] For example, a lexical unit sequence is a sequence of words obtained by performing lexical analysis on the characters of the source code.
[0112] For example, an abstract syntax tree is an abstract representation of the syntax structure of source code. An abstract syntax tree represents the syntax structure of a programming language in a tree-like form, and each node in the tree represents a syntax structure in the source code.
[0113] For example, a three-address code may include two operand address codes and a result address code, so that the original operands are not changed after the operation is completed.
[0114] For example, a directed acyclic graph is a directed graph without loops.
[0115] For example, in the static analysis tool Soot, an application can be converted into one or more intermediate representations, each of which can be any type of linear intermediate representation or any type of graph intermediate representation.
[0116] For example, a linear intermediate representation may include one or more of the following: a three-address code representation, or a static single assignment representation. A graph intermediate representation may include one or more of the following: a method call graph, a module call graph, or a control flow graph.
[0117] For example, a method call graph is a directed graph with methods as nodes and method calls as edges, used to represent the calling relationship between methods.
[0118] For example, a module call graph is used to represent the call relationships between application modules.
[0119] For example, a control flow graph is used to represent the calling relationships between basic blocks or instructions. A control flow graph represents all possible execution paths in a program segment in graph form. Specifically, a control flow graph can include a unit control flow graph and a block control flow graph. In a unit control flow graph, a node represents a unit, such as a single instruction. In a block control flow graph, a node represents a basic block, such as a set of one or more instructions without branches.
[0120] In one possible design, the above-mentioned S201, determining the intermediate representation of the application, may include the following S201-1 and S201-2.
[0121] S201-1, Obtain the linear intermediate representation of the application.
[0122] Optionally, S201-1, obtaining the linear intermediate representation of the application may include: obtaining the application's installation package, determining the executable code in the installation package, and then obtaining the linear intermediate representation based on the executable code in the installation package.
[0123] For example, executable code can be code that a machine can execute directly.
[0124] The following uses an APK as an example to illustrate the detailed process of obtaining the linear intermediate representation of an application.
[0125] For example, the executable code of the installation package can be obtained from the APK's dex file (a file in dex format). Specifically, the Soot tool can be used to parse the dex file and create jimple code based on the code in the parsed dex file.
[0126] For example, the dex2jar tool can be used to obtain Java bytecode (files in .class format, hereinafter referred to as .class files). Specifically, an APK file can be input into dex2jar to decompress the APK file, and the dex file can be extracted from the decompressed APK file and converted into Java bytecode (.class files). Then, the dex2jar tool packages all the .class files corresponding to the Java bytecode to obtain a .jar file.
[0127] After obtaining the .jar file, Soot can be used to retrieve a linear intermediate representation of the .jar file, such as Jimple code or Shimple code. Jimple code is an intermediate representation using a three-address code representation. Shimple code is an intermediate representation using a static single-assignment representation.
[0128] For example, when using Soot to convert a .jar file into Jimple and Shimple code, the following code can be used:
[0129] Options.v().set_src_prec(Options.src_prec_class);
[0130] Options.v().set_output_format(Options.output_format_jimple); / / outputjimple
[0131] Options.v().set_output_format(Options.output_format_shimple); / / outputshimple
[0132] For example, when using the Soot tool to obtain the Jimple and Shimple code from a .jar file, the Jimple and Shimple code can also be obtained through the Soot tool's command line. The specific commands are as follows:
[0133] java-jar <jar-file>\#the tool's executable jar
[0134] -a<android.jar>\#android SDK location
[0135] -i <apk-file>\#the input APK
[0136] -o <output-directory>#output directory
[0137] Therefore, using the dex2jar tool to obtain Java bytecode and then using that Java bytecode to obtain the linear intermediate representation of Soot can improve the conversion speed of the Soot tool, thereby improving the efficiency of obtaining the linear intermediate representation.
[0138] Alternatively, in step S201-1 above, obtaining the linear intermediate representation of the application may include: obtaining the source code of the application and obtaining the corresponding linear intermediate representation based on the source code of the application.
[0139] The following section uses an Android application to illustrate the detailed process of obtaining the linear intermediate representation of the application.
[0140] For example, the application source code can be transformed using the open-source tool Gradle, which automates project builds. Gradle can be used to package the application. Specifically, a program capable of obtaining a linear intermediate representation can be integrated into the installation package packaging process. A Gradle plugin can be added to the `build.gradle` script in the Android project directory, along with the relevant parameters. After adding the Gradle plugin and setting the parameters, the `gradle build` command can be executed via the command line to run the corresponding project instance, thereby obtaining a linear intermediate representation of the source code, such as Jimple or Shimple code, in the root directory of the Android project. Adding the Gradle plugin to the `build.gradle` script in the Android project directory can be achieved using the following code, where different lines of code are separated by newlines:
[0141]
[0142] The process of transforming application source code using Gradle can be summarized as follows: Gradle reads the include information from the `settings.gradle` file in the project's root directory to determine how many projects will be included in the build, and creates project instances. The `settings.gradle` file allows for customization of build tasks. Each `build.gradle` file in the project corresponds to a project instance. Then, Gradle configures Gradle objects based on the `build.gradle` files in each project directory and constructs a directed graph of task dependencies. Finally, based on the obtained configuration information, the directed graph of task dependencies, and the new build tasks, Gradle executes the corresponding tasks sequentially.
[0143] Specifically, a new build task can be added to Gradle, specifying that it depends on Gradle's built-in build tasks. This new build task can include retrieving the APK corresponding to the application's source code. Gradle executes tasks sequentially based on the obtained configuration information and the task dependency graph, with the new build task executing after Gradle's built-in build tasks. When the new build task executes, it can output the project's APK to the output directory. The Gradle plugin obtains the user-defined APK output path, the Android SDK directory in the system, and other information configured by the user in `build.gradle` from the project instance. This information is then passed as parameters to the program used to retrieve the linear intermediate representation, and the program is executed. Thus, after the Android application project is successfully built, its corresponding intermediate representation will be generated in the specified directory. The principle behind retrieving the linear intermediate representation is similar to that of retrieving the linear intermediate representation from the installation package, and will not be elaborated here.
[0144] Furthermore, the above-mentioned S201, determining the intermediate representation of the application, may also include S201-2.
[0145] S201-2, Obtain the graph intermediate representation based on the linear intermediate representation.
[0146] For example, the diagram representation may include one or more of the following: method call graph, control flow graph, or module call graph.
[0147] Optionally, S201-2 above, obtaining the graph intermediate representation based on the linear intermediate representation, may include step 1.
[0148] Step 1: Obtain the method call graph based on the linear intermediate representation.
[0149] Taking the Soot tool as an example, the method call graph of a program can be constructed during the call graph (cg) phase. In other phases after the call graph phase, such as the whole-jimple transformation pack (wjtp), whole-jimple optimization pack (wjop), whole-jimple annotation pack (wjap), and jtp transformation, the method call graph can be obtained using the following code:
[0150] Scene.v().getCallGraph();
[0151] Furthermore, step 2 may be included in S201-2 above, which obtains the graph intermediate representation based on the linear intermediate representation.
[0152] Step 2: Obtain the control flow graph based on the linear intermediate representation.
[0153] Specifically, the control flow graph can be obtained from either Jimple code or Shimple code.
[0154] For example, a unit control flow graph can be obtained using `new ExcaptionalUnitGraph()`, and a block control flow graph can be obtained using `new ExcaptionalBlockGraph()`. For instance, the code to obtain the unit and block control flow graphs based on the Jimple and Shimple code is as follows:
[0155] Body body = ;
[0156] ExceptionalUnitGraph unitGraph=new ExcaptionalUnitGraph(body);
[0157] ExceptionalBlockGraph blockGraph=new ExcaptionalBlockGraph(body);
[0158] Understandably, after obtaining the control flow graph, the corresponding .dot file can be obtained through the DotGrap class of the Soot tool to visualize the control flow graph.
[0159] Furthermore, step 3 may also be included in S201-2 above, which obtains the graph intermediate representation based on the linear intermediate representation.
[0160] Step 3: Obtain the module call graph based on the linear intermediate representation.
[0161] For example, the Android system includes four main categories of Android components: activities, services, broadcast receivers, and content providers. These components communicate with each other by calling relevant application program interfaces (APIs) and sending intent objects. An intent object is an information object used for communication between Android components. Through an intent object, an application can express a request or operation to the Android system, such as starting an activity or service, or sending a broadcast. The Android system can select the appropriate Android component to respond based on the operation content contained in the intent object. Therefore, the mutual calling relationships between Android components can be obtained by analyzing the API calls and intent object sending between them. Furthermore, the Android system includes intent filters associated with intent objects. Intent filters describe which intent objects an Android component can manipulate. After the Android system receives an intent object from an Android component, it searches and matches Android components that can handle that intent object based on each component's intent filter. Intent object filters can be defined in the AndroidManifest.xml file.
[0162] For example, when obtaining the mutual startup relationships between three Android components—activity, service, and broadcast receiver—after decompressing the APK file, you can obtain all component information in the APK by analyzing the AndroidManifest.xml configuration file of the Android application. Then, you can use Soot to obtain all the classes defined in the application. Based on the component information, all the classes defined in the application obtained by Soot, and the pre-set output path, you can obtain the module call graph.
[0163] Specifically, the process of obtaining the module call graph for three components—activities, services, and broadcast receivers—can include: obtaining the call relationships of components. For example, the callback function and constructor of an Android component can be used as the module entry point, and the three-level method call can be recursively checked downwards to obtain all the methods called. For each statement in a method, it is checked whether the statement is a broadcast receiver dynamic registration statement or a module call interface. If the checked statement is a broadcast receiver dynamic registration statement or a module call interface, a backward slicing operation is further performed on the parameters of the intent filter or intent object passed in the statement to find all statements related to that variable, tracing back to its declaration starting point and all operations in the acquisition process. During backward slicing, if a temporary variable returned by a method is detected, the method is traced deeper; if a variable defining a parameter is encountered, the upper-level calling function in the method call stack is traced back. In this way, the definition location of the intent object can be found.
[0164] For example, when obtaining the call relationship between components, the Android system's application program interface (API) can be detected. For instance, the following Android system APIs can be detected: startActivity, startActivityForResult, startActivityFromChild, startActivityFromFragment, startActivityIfNeeded, startService, bindService, startForegroundService, sendBroadcast, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, and sendStickyOrderedBroadcastAsUser.
[0165] Table 1
[0166] startActivity startActivityForResult startActivityFromChild startActivityFromFragment startActivityIfNeeded startService bindService startForegroundService sendBroadcast sendBroadcastAsUser sendOrderedBroadcast sendOrderedBroadcastAsUser sendStickyBroadcast sendStickyBroadcastAsUser sendStickyOrderedBroadcast sendStickyOrderedBroadcastAsUser
[0167] For example, when performing reverse slicing on the parameters of an intent object, the APIs of the intent object class can be detected, such as those shown in Table 2: setComponent, setClassName, setClass, setAction, addCategory, setType, setTypeAndNormalize, setData, setDataAndNormalize, setDataAndType, setDataAndTypeAndNormalize, setType, setTypeAndNormalize, createChooser, makeMainActivity, makeRestartActivityTask, makeMainSelectorActivity, getIntent, getIntentOld, parseUri, etc.
[0168] Table 2
[0169] setComponent setClassName setClass setAction addCategory setType setTypeAndNormalize setData setDataAndNormalize setDataAndType setDataAndTypeAndNormalize setType setTypeAndNormalize createChooser makeMainActivity makeRestartActivityTask makeMainSelectorActivity getIntent getIntentOld parseUri
[0170] When performing reverse slicing on the parameters of an intent filter, you can inspect the APIs defined in the intent filter class. For example, the APIs shown in Table 3 are: addAction, addCategory, addDataType, addDataScheme, addDataSchemeSpecificPart, addDataPath, and addDataAuthority.
[0171] Table 3
[0172]
[0173]
[0174] By examining the APIs of the Android system, the APIs of the intent object class, and the APIs of the intent filter class, we can obtain an edge (the intent object and its internal information) and a node (a module composed of Android components) in the module call graph. Then, based on the way the Android system parses intent objects, we can obtain the information contained in the intent object, thereby obtaining the other node of that edge. In this way, the module call graph can be obtained.
[0175] For example, the Android system can parse intent objects in one or more of the following ways: for explicit intents that have specified a component, the specified component will be launched directly; or for implicit intents that have not specified a component, the intent filter will be checked to see if it matches the fields in the intent object, such as action, category, and data (if they exist).
[0176] For example, the Android system determines the match between the intent filter and the action, category, and data (if any) in the intent object using one of the following rules:
[0177] Any action in the intent filter matches an action in the intent object; or, a category in the intent filter matches a category in the intent object; or, any data type in the intent filter matches a data type in the intent object; or, any data scheme in the intent filter matches a data scheme in the intent object; or, the intent filter has no data authority, or any data authority in the intent filter matches a data policy in the intent object, and any data scheme in the intent filter matches a data scheme in the intent object; or, the intent filter has no data path information, or any data path in the intent filter matches a data path in the intent object, any data scheme in the intent filter matches a data scheme in the intent object, any data authority in the intent filter matches a data policy in the intent object, and the intent filter has no data authority information. For example, the pseudocode (a language describing the module structure diagram) for constructing a module call graph including three components: activity, service, and broadcast receiver is as follows:
[0178]
[0179]
[0180] S202, obtain the loop body based on the intermediate representation.
[0181] For example, the loop body can be code that is executed repeatedly.
[0182] Specifically, the loop body can be obtained from one or more of the following: module call graph, method call graph, or control flow graph.
[0183] For example, the above-mentioned S202, obtaining the loop body according to the intermediate representation, may include one or more of steps 4, 5, or 6.
[0184] Step 4: Obtain the loop body based on the module call graph.
[0185] After obtaining the module call graph, the loop body can be obtained by finding strongly connected components in the graph. The Tarjan algorithm can be used to find strongly connected components; the principle behind Tarjan's algorithm for finding strongly connected components can be found in existing technologies and will not be elaborated here.
[0186] For example, to find the strongly connected components of a directed graph with |V| nodes and |E| edges in O(|V|+|E|) time complexity, the pseudocode for Tarjan's algorithm is as follows:
[0187] Algorithm: Tarjan
[0188] Input: Graph G = (V, E)
[0189] Output: The set of vertices partitioned by their strongly connected components.
[0190]
[0191]
[0192] Step 5: Obtain the loop body based on the method call graph.
[0193] In one possible design, the diagram in the middle may include a method call graph. The method call graph is used to represent the calling relationships between methods.
[0194] The method call graph is also a directed graph. Step 5, obtaining the loop body from the method call graph, is similar in principle to obtaining the loop body from the module call graph; it can also be achieved by finding strongly connected components in the directed graph. The principle for obtaining the loop body from the method call graph can be referenced from the principle for obtaining the loop body from the module call graph, and will not be elaborated here.
[0195] In another possible implementation, step 5 above, obtaining the loop body according to the method call graph, may include steps 51-52.
[0196] Step 51: Perform a depth-first search for each node in the method call graph to obtain strongly connected components.
[0197] Each node represents a method.
[0198] For example, starting with each method, a depth-first search is performed, using that method as the starting point for the search.
[0199] Optionally, when performing a depth-first search, a depth-first search with a depth limit can be used. For example, the search depth can be limited to 5 levels. Exemplarily, the search depth can be manually specified via command-line arguments.
[0200] Step 52: Determine the loop body based on the strongly connected components.
[0201] It should be noted that each strongly connected component detected in the method call graph can correspond to a loop body formed by a method recursion. In other words, the loop body in the method call graph can be a method recursion.
[0202] Thus, searching independently for each method allows for simultaneous searches across multiple nodes, enabling concurrent processing and improving the efficiency of detecting infinite loops. Employing depth-first search with depth constraints can further enhance the efficiency of depth-first search.
[0203] Step 6: Obtain the loop body based on the control flow graph.
[0204] For example, step 6 above, obtaining the loop body based on the control flow graph, may include steps 61-64.
[0205] Step 61: Calculate the dominance relationships between all nodes in the control flow graph.
[0206] For example, domination means that for any given pair of nodes A and B, if reaching node B from the starting point requires passing through node A, then "A dominates B".
[0207] Step 62: Construct a depth-first search tree based on the dominance relationships among all nodes.
[0208] After calculating all dominance relationships, a depth-first search operation is performed on the entire graph starting from the beginning of the control flow graph, which can construct a depth-first search tree.
[0209] Step 63: Obtain the reverse edges on the depth-first search tree based on the dominance relationship.
[0210] Understandably, a depth-first search tree can also include forward edges and cross edges.
[0211] Step 64: Obtain the loop body corresponding to each reverse edge.
[0212] It is understandable that each reverse edge corresponds to a path in the loop.
[0213] Figure 3 It is a depth-first search tree of the control flow graph, combined with the following Figure 3 This section details the process of performing a depth-first search in the control flow graph.
[0214] like Figure 3 The depth-first search tree shown contains 10 nodes, numbered 1 to 10. Edges such as those from node 4 to node 3 and node 7 to node 4 are reverse edges. For each reverse edge, a depth-first search is performed again on the reverse graph of the control flow graph, with the starting point of the reverse edge as the search start point and the ending point as the search end point. Each node obtained in this way is a node in the cycle path corresponding to that reverse edge. It should be noted that the principle of performing depth-first search on the reverse graph of the control flow graph is similar to that of performing depth-first search on the control flow graph itself, and will not be elaborated further here.
[0215] For example, the pseudocode for obtaining the loop body from the control flow graph is as follows:
[0216]
[0217] Step 6 allows you to identify loops in the control flow graph. It's important to note that the loops identified through the control flow graph can be natural loops, such as those constructed using for, if-else, while, continue, break, or even goto statements.
[0218] Understandably, after identifying the loop body in the control flow graph, it's also possible to visualize it. For example, Soot's built-in graph module can be used to obtain the .dot file corresponding to the loop body, and the Graphviz tool can be used to visualize the .dot file. For instance, in the following code, the natural loop corresponding to the while statement can be visualized:
[0219]
[0220]
[0221] S203, It is determined that there is a risk of the loop body entering an infinite loop.
[0222] For example, an infinite loop can occur when a program cannot terminate itself.
[0223] In one possible design, S203 above, determining that the loop body has the risk of an infinite loop, may include S203-1 and S203-2.
[0224] S203-1, Determine the dependencies between variables in the loop body.
[0225] For example, in Soot's linear intermediate representation, loop control is implemented through jump statements. In other words, in Soot's linear intermediate representation, the exit of each loop is achieved by an if statement followed by a goto statement. Therefore, if a subsequent statement of an if statement is not within the loop body, then the if statement is an exit point for the loop, and the condition of the if statement is the exit condition of the loop. For example, in the shimple code corresponding to the following Java code, the code "goto label 1" after the if statement is not within the loop body. Therefore, the exit condition of the loop corresponding to this Java code is "i0>=10".
[0226] / / Java code
[0227] int x = 10;
[0228] int y = 5;
[0229] while(x<10){
[0230] y = 10;
[0231] }
[0232] / / Shimple code
[0233] i0 = 10;
[0234] i1 = 5;
[0235] label 0:
[0236] if i0>=10 goto label 1;
[0237] goto label 0;
[0238] For example, the dependencies between variables in the code corresponding to the loop body can be determined using intraprocedural, finite, distributed, subset (IFDS) methods. IFDS is a widely used data analysis framework. When determining the dependencies between variables in the code corresponding to the loop body using the IFDS framework, the IFDS framework visits each node in the control flow graph in the order of control flow jumps, updating the state of each node as it visits it, until the state of all nodes no longer changes (fixed-point iteration process).
[0239] For example, in Soot, this can be achieved by inheriting from ForwardFlowAnalysis.<N,A> This class, and override ForwardFlowAnalysis<N,A> The methods in this class perform forward data flow analysis for IFDS. For example, the following methods can be overridden: `newInitialFlow()`, `copy(A src, Attg)`, `merge(A left, A right, A out)`, and `flowThrough(A in, N node, A out)`. `newInitialFlow()` creates an empty state object before accessing each node to store the dependencies of variables within that node. `copy(A src, Attg)` copies the state object. `merge(A left, A right, A out)` merges the state objects passed from both sides when merging branches in the control flow graph. `flowThrough(A in, N node, A out)` retrieves the input and output data of the currently accessed node when accessing a node in the control flow graph.
[0240] For example, the following operations can be repeated until the set of state objects no longer changes: The IFDS framework iterates through the nodes in the control flow graph. Before IFDS traverses each node, a state object is created using the `newInitialFlow()` method, then the node is analyzed and its state is updated. The input and output of the current node are obtained using the `flowThrough(A in, N node, A out)` method, and the dependencies of variables are obtained based on the input and output of the current node.
[0241] For example, when traversing each node, assignment statements can be used to analyze the node. For instance, when encountering an assignment statement, the variable on the left side of the equals sign is mapped to the variable included on the right side, forming a variable dependency. Simultaneously, when encountering a loop condition, the variables that the loop condition depends on are parsed from the control flow graph. If a node has more than one predecessor node, the control flow from the predecessor node can be merged using `merge(A left, A right, A out)`. That is, when the control flow converges, a union operation is performed on the nodes visited by different branches to obtain the variable dependencies of the current node. Then, the state object can be copied to the state object collection using `copy(A src, A tgt)` to add new dependencies or update existing dependencies within the state object collection.
[0242] For example, a lattice is a set of state objects that is partially ordered, with a maximum lower bound and a minimum upper bound for each non-empty subset. In the IFDS framework, lattices can be used to store the current state and ensure that the state value changes monotonically with each node traversal. These two points ensure that the data flow analysis will converge. For a lattice S, assuming the function F is monotonically increasing, iterating over F on the elements of a set will eventually reach the upper bound. In this embodiment, a lattice is defined as the set of nodes visited in the IFDS analysis.
[0243] S203-2, If the first condition is met, it is determined that there is a risk of the loop body entering an infinite loop.
[0244] The first condition is determined based on dependencies. The first condition may include: the loop variable within the loop body has not been assigned a value.
[0245] For example, if the loop variable in the loop body is 'a', and the assignment statement for the loop variable is 'a = b + c', but 'b' and / or 'c' are not assigned values in the loop body, it means that the loop variable in the loop body is not assigned a value in the loop body. In other words, the loop expression will not be updated.
[0246] Therefore, by analyzing the dependencies of each variable to determine the variables that the loop variable depends on, the detection efficiency of infinite loops can be improved.
[0247] In another possible design, the linear intermediate representation may include a static single-assignment representation. The aforementioned S203, determining the risk of an infinite loop within the loop body, may include S203-3 and S203-4.
[0248] S203-3, Determine the static single-assignment representation of the loop body.
[0249] For example, a Java variable that is assigned multiple times will correspond to a variable in multiple static single assignment representations.
[0250] For example, the following Java code:
[0251]
[0252] S203-4, If the second condition is met, it is determined that there is a risk of the loop body entering an infinite loop.
[0253] The second condition may include: all variables included in the first loop condition in the static single assignment representation have been assigned values.
[0254] For example, an infinite loop can occur when an application cannot terminate itself under its own control.
[0255] For example, the initial loop condition is the loop condition when the loop body is first executed. For instance, "if i_2>=10" when the loop body is first executed in SSA of S203-3 is the initial loop condition.
[0256] Continuing with the SSA example from S203-3, each time a variable in the Java code is assigned a new value, a new variable appears in the corresponding SSA. For instance, for the variable `x` in the Java code, different assignments in the two branches of the `if` statement result in two corresponding variables, `x_1` and `x_2`, in the SSA. Similarly, for the loop variable `i` in the Java code, `i_1` appears in the SSA when it's initially assigned a value, and `i_2` appears when it's reassigned within the loop body. For the two sets of separately defined variables `x_1` and `x_2`, and `i_1` and `i_2`, the SSA uses Phi expressions to merge them, such as `"x_3 = Phi(x_1, x_2)"` and `"i_2 = Phi(i_1, i_3)"`. Here, `x_3` is the result of merging `x_1` and `x_2`.
[0257] When the program executes the `then` branch of the `if` statement, `x_3` becomes the value of `x_1`. When the program executes the `else` branch of the `if` statement, `x_3` becomes the value of `x_2`. The principle for determining the value of the loop variable `i_2` is similar to that for `x_3`, and will not be elaborated here. In the SSA of S203-3, the variables `x` corresponding to different branches of the `if` statement in the Java code are already defined when the Phi expression is executed. However, the variable `i` in the loop body of the Java code is not defined when the Phi expression is first executed in the SSA. `i_3` is only defined after the loop body has been executed once. In other words, when the loop condition is executed for the first time in the SSA code, if the loop condition depends on a Phi expression containing undefined parameters, then the loop condition must have been updated in the loop body; otherwise, the loop condition has not been updated in the loop body.
[0258] Therefore, determining the risk of an infinite loop directly based on the static single-assignment representation of the loop body can reduce computational load and improve the efficiency of infinite loop detection. Furthermore, analyzing the variable assignments in the initial loop condition can further improve the accuracy of infinite loop detection.
[0259] In another possible design, the above S203, which determines that the loop body has the risk of dead loop, may include S203-5 and S203-6.
[0260] S203-5, Determine the change in the cyclic body.
[0261] The change is the difference between the symbolic expressions during two consecutive executions of the loop body. During the execution of the loop body, unknown loop variables are represented by the first variable.
[0262] For example, the symbolic expression can be an expression that includes all constants and variables in the loop condition. For instance, if the loop condition is x+y<5, then the symbolic expression is x+y-5.
[0263] For example, if the update statement for the loop variable is "n = n + 5", the symbolic expression is "n + m" before the loop body is executed for the first time, and the symbolic expression is "2n + m + 5" after the loop body is executed for the first time. The change is "2n + m + 5 - (n + m)", which is "n + 5".
[0264] For example, an unknown loop variable is a loop variable whose specific value cannot be determined when the loop body is executed alone. For instance, in the symbolic expression "n+m", if the value of m can be determined, but the value of n depends on the execution result of other code, then n is an unknown loop variable.
[0265] For example, Figure 4 For a control flow graph of a natural cycle, such as Figure 4 As shown, a natural loop can include a loop header, a loop exit point, and loop body statements. In S203-5 above, determining the change in the loop body can include: obtaining the loop header, loop exit point, and loop body statements; then obtaining the loop path based on the loop header, loop exit point, and loop body statements; and determining the change in the loop body based on the loop path. Specifically, the loop header is used to assign values to the loop condition, the loop exit point is used to determine whether the loop condition is met, the loop body statements contain all statements within the loop (which may include nested loops), and finally, a jump statement returns to the loop header.
[0266] Figures 5-7 This is a schematic diagram of the loop path, such as... Figures 5-7 As shown, loop paths can be divided into three categories. Figure 5 The starting point of the loop path is the loop head, and the ending point returns to the loop starting point of this loop body. Figure 6 The starting point of the loop path is the loop head, and the ending point jumps to the starting point of another loop body. Figure 7 The loop path in the loop starts at the loop head and ends outside the loop body.
[0267] For example, determining the change in the loop body based on the loop path may include: performing program slicing on the linear intermediate representation corresponding to the loop path to obtain instructions related to the loop variables included in the loop path. Then, the execution process of the loop path is simulated using symbolic execution to obtain the change. The principle of simulating the execution of the loop path can refer to the existing principles of symbolic execution, and will not be elaborated here.
[0268] S203-6, If the third condition is met, it is determined that there is a risk of the loop body entering an infinite loop.
[0269] The third condition is determined based on the first variable, the loop condition, the initial condition, and the amount of change.
[0270] For example, an infinite loop can occur when a program cannot terminate itself.
[0271] For example, the risk of an infinite loop can be assessed by determining the termination status of the loop path. A loop path is considered terminateable if, after a certain number of executions, it can transition to another loop path or exit the loop. A loop path is considered non-terminable if, after a certain number of executions, it cannot transition to another loop path. If a loop path is non-terminable or there are unstoppable transitions between different loop paths, then the loop body is at risk of becoming an infinite loop. For example, in the following code loop body, both the loop path corresponding to the while statement and the loop path corresponding to the if statement are terminateable, but there is an unstoppable transition between these two loop paths.
[0272]
[0273] Specifically, a non-terminating loop path can be determined based on the third condition, or there can be non-terminating mutual conversions between different loop paths.
[0274] Therefore, detecting dead loops based on the third condition can improve the detection efficiency of dead loops.
[0275] Alternatively, the third condition may include one of the following:
[0276] There exists a value for the first variable such that the initial condition is satisfied, and the sign of the variable is the same as the sign of the loop condition. The sign includes one of the following: positive, non-negative, 0, non-positive, or negative. Alternatively,
[0277] There exists a value for the first variable such that the initial condition is satisfied when the sign of the loop condition is not 0, and the initial condition has either a positive or negative sign, and the change is 0. Alternatively,
[0278] There exists a first variable whose value allows the loop condition to have both positive and negative signs, the initial condition to have a non-zero sign, and the initial condition to be a non-integer multiple of the variable.
[0279] For example, the sign of the loop condition can be the sign of the symbolic expression of the loop condition. The sign of the initial condition can be the sign of a possible value of the first variable substituted into the initial condition. The sign of the initial condition is the same as the sign of the loop condition, which can be the same as the sign of the symbolic expression of the loop condition, or the sign of the possible value of the first variable substituted into the initial condition is one of the signs of the symbolic expression of the loop condition. The sign of the variable is the sign of the variable after substituting the value of the first variable into the variable. The sign of the variable is the same as the sign of the loop condition, which can be the same as the sign of the symbolic expression of the loop condition, or the sign of the variable after substituting the value of the first variable into the variable is 0.
[0280] For example, Table 4 shows the sign relationship between the initial condition, the loop condition, and the change when the initial condition is met. As shown in Table 4, when the loop condition f(x) > 0, the initial condition f(x0) > 0, and the change δ > 0. When the loop condition f(x) >= 0, the initial condition f(x0) >= 0, and the change δ >= 0. When the loop condition f(x) = 0, the initial condition f(x0) = 0, and the change δ = 0. When the loop condition f(x) ! = 0, the initial condition f(x0) < 0, and the change δ < 0. When the loop condition f(x) ! = 0, the initial condition f(x0) > 0, and the change δ > 0. When the loop condition f(x) < 0, the initial condition f(x0) < 0, and the change δ < 0. When the loop condition f(x) <= 0, the initial condition f(x0) <= 0, and the change δ <= 0.
[0281] For example, the sign of the loop condition is not zero; it can be that the sign of the symbolic expression of the loop condition is positive and / or negative. The initial condition is satisfied; the sign of the initial condition can be the sign of any possible value of the first variable substituted into the initial condition. The sign of the initial condition is the same as the sign of the loop condition; it can be that the sign of any possible value of the first variable substituted into the initial condition is the same as the sign of the symbolic expression of the loop condition, or the sign of any possible value of the first variable substituted into the initial condition is one of the signs of the symbolic expression of the loop condition. The change is zero; it can be that the sign of the change after substituting the value of the first variable into the change is zero.
[0282] Taking Table 4 as an example, as shown in Table 4, when the loop condition f(x) > 0, the initial condition f(x0) > 0, and the change δ = 0. When the loop condition f(x) < 0, the initial condition f(x0) < 0, and the change δ = 0. When the loop condition f(x) ! = 0, the initial condition f(x0) > 0, and the change δ = 0. When the loop condition f(x) ! = 0, the initial condition f(x0) < 0, and the change δ = 0.
[0283] For example, the sign of the loop condition can be positive or negative, which can be the sign of the symbolic expression of the loop condition. The initial condition is satisfied if the sign of the value of the first variable substituted into the initial condition is either positive or negative. The initial condition is a non-integer multiple of the variable, meaning the initial condition is not divisible by the variable.
[0284] Taking Table 4 as an example, as shown in Table 4, the loop conditions are f(x)! = 0, f(x0) > 0, and |f(x0)|%|δ|! = 0. Alternatively, the loop conditions are f(x)! = 0, f(x0) < 0, and |f(x0)|%|δ|! = 0.
[0285] Table 4
[0286] line number Loop condition initial value Change 1 f(x)>0 f(x0)>0 δ≥0 2 f(x)≥0 f(x0)≥0 δ≥0 3 f(x) = 0 f(x0)==0 δ==0 4 f(x)! = 0 f(x0)>0 δ≥0 5 f(x)! = 0 f(x0)<0 δ≤0 6 f(x)! = 0 f(x0)<0 <![CDATA[|f(x0)|%|δ|!=0]]> 7 f(x)! = 0 f(x0)>0 <![CDATA[|f(x0)|%|δ|!=0]]> 8 f(x)<0 f(x0)<0 δ≤0 9 f(x)≤0 f(x0)≤0 δ≤0
[0287] Thus, when the value of the first variable is determined, judging the relationship between the initial conditions, loop conditions, and transformation quantities can identify dead loops and improve the detection efficiency of dead loops.
[0288] based on Figure 2 The dead loop detection method shown obtains the loop body based on the intermediate representation of the application and determines whether the loop body has a risk of dead loop. For example, the dead loop risk in the loop body can be determined directly from the application code or application installation package. This can avoid the influence of CPU load status on the dead loop detection results, thereby improving the detection accuracy of dead loop.
[0289] Furthermore, the solution provided in this application avoids continuously monitoring CPU usage or the execution status of scheduled tasks during application operation when detecting infinite loops, which can reduce resource overhead and thus improve CPU efficiency.
[0290] It should be noted that S203 above can be applied to loop bodies obtained from any of the module call graphs, method call graphs, and control flow graphs. Determining the risk of an infinite loop based on a loop body obtained from any of these graphs is similar to the principles in S203-1 and S203-2, S203-3 and S203-4, or S203-5 and S203-6, and will not be elaborated further here.
[0291] The above combination Figures 2-7 The method for detecting dead loops provided in the embodiments of this application is described in detail below. Figures 8-9 This document describes in detail the dead loop detection apparatus used to perform the dead loop detection method provided in the embodiments of this application.
[0292] For example, Figure 8 This is a schematic diagram of the dead loop detection device provided in the embodiments of this application. Figure 1 .like Figure 8 As shown, the dead loop detection device 800 includes: a determination module 801 and an acquisition module 802. For ease of explanation, Figure 8 Only the main components of this dead loop detection device are shown.
[0293] In some embodiments, the dead loop detection device 800 can be used Figure 1 In the terminal device shown, the execution Figure 2 The function in the dead loop detection method shown.
[0294] The determination module 801 is used to determine the intermediate representation of the application.
[0295] The intermediate representation is determined based on the application's source code and / or installation package, and includes linear intermediate representation and graph intermediate representation.
[0296] The acquisition module 802 is used to obtain the loop body based on the intermediate representation.
[0297] The determination module 801 is also used to determine the risk of the loop body entering an infinite loop.
[0298] In one possible design, the diagram includes a method call graph. The method call graph represents the calling relationships between methods.
[0299] The acquisition module 802 is also used to perform a depth-first search on each node in the method call graph to obtain strongly connected components, and to determine the loop body based on the strongly connected components. Each node represents a method.
[0300] In one possible design, module 801 is also used to determine the dependencies between variables in the loop body.
[0301] The determination module 801 is also used to determine whether the first condition is met and to determine whether there is a risk of an infinite loop in the loop body. The first condition is determined based on dependencies. The first condition includes: the loop variable in the loop body has not been assigned a value within the loop body.
[0302] In another possible design, the linear intermediate representation may include a static single-assignment representation.
[0303] The determining module 801 is also used to determine the static single-assignment representation of the loop body. The determining module 801 is also used to determine that the second condition is met and to determine that the loop body has a risk of entering an infinite loop. The second condition includes: all variables included in the initial loop condition of the static single-assignment representation have been assigned values.
[0304] In another possible design, module 801 is also used to determine the change in the loop body. The change is the difference in the loop condition between two consecutive executions of the loop body. During the execution of the loop body, the unknown loop variable is represented by the first variable.
[0305] The determination module 801 is also used to determine that the third condition is met and to determine that there is a risk of an infinite loop in the loop body, wherein the third condition is determined based on the first variable, the loop condition, the initial condition, and the change amount.
[0306] Optionally, the third condition may include one of the following: there exists a value for the first variable such that the initial condition is satisfied, and the sign of the change is the same as the sign of the loop condition. The sign includes one of the following: positive, non-negative, 0, non-positive, or negative. Alternatively, there exists a value for the first variable such that the initial condition is satisfied when the sign of the loop condition is not 0, the initial condition has a positive or negative sign, and the change is 0. Alternatively, there exists a value for the first variable such that the sign of the loop condition includes both positive and negative, the sign of the initial condition is not 0, and the initial condition is a non-integer multiple of the change.
[0307] It should be noted that the determination module 801 and the acquisition module 802 can also be integrated into a single module, such as a processing module. The processing module is used to implement the operations of the aforementioned functional modules.
[0308] Optionally, the dead loop detection device 800 may also include a storage module. Figure 8 (Not shown in the image), this storage module stores programs or instructions that, when executed by the processing module, enable the dead loop detection device 800 to execute. Figure 2 The dead loop detection method shown is illustrated.
[0309] Optionally, the dead loop detection device 800 may also include a transceiver module. Figure 8 (Not shown in the image). The transceiver module is used to implement... Figure 2 The transmit and receive functions of the dead loop detection device 800 shown.
[0310] Furthermore, the transceiver module may include a receiving module and a sending module. The receiving module is used to implement the receiving function of the dead loop detection device 800, and the sending module is used to implement... Figure 2 The transmission function of the dead loop detection device 800 shown.
[0311] It should be noted that the dead loop detection device 800 can be a terminal device or a network device, or it can be a chip (system) or other component or assembly that can be set in a terminal device or a network device, or it can be a device that includes a terminal device or a network device. This application does not limit it in this regard.
[0312] In addition, the technical effectiveness of the dead loop detection device 800 can be referenced. Figure 2 The technical effects of the dead loop detection method shown are not elaborated here.
[0313] For example, Figure 9 Schematic diagram of the dead loop detection device provided in the embodiments of this application Figure 2 The dead loop detection device 900 can be a terminal device or a network device, or it can be a chip (system) or other component or assembly that can be installed in the terminal device or network device. Figure 9 As shown, the dead loop detection device 900 may include a processor 901. Optionally, the dead loop detection device 900 may also include a memory 902 and / or a transceiver 903. The processor 901 is coupled to the memory 902 and the transceiver 903, for example, via a dead loop detection bus.
[0314] The following is combined Figure 9 A detailed description of each component of the dead loop detection device 900 is provided below:
[0315] The processor 901 is the control center of the dead loop detection device 900. It can be a single processor or a collective term for multiple processing elements. For example, the processor 901 can be one or more central processing units (CPUs), application-specific integrated circuits (ASICs), or one or more integrated circuits configured to implement the embodiments of this application, such as one or more digital signal processors (DSPs), or one or more field-programmable gate arrays (FPGAs).
[0316] Optionally, the processor 901 can perform various functions of the dead loop detection device 900 by running or executing software programs stored in the memory 902 and calling data stored in the memory 902.
[0317] In a specific implementation, as one example, the processor 901 may include one or more CPUs, for example... Figure 9 CPU0 and CPU1 are shown in the diagram.
[0318] In a specific implementation, as one example, the dead loop detection device 900 may also include multiple processors, for example... Figure 2 The processors 901 and 904 are shown. Each of these processors can be a single-core processor or a multi-core processor. Here, "processor" can refer to one or more devices, circuits, and / or processing cores used to process data (e.g., computer program instructions).
[0319] The memory 902 is used to store the software program that executes the solution of this application, and is controlled by the processor 901 to execute it. The specific implementation method can be referred to the above method embodiment, and will not be repeated here.
[0320] Optionally, the memory 902 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and instructions, random access memory (RAM) or other type of dynamic storage device capable of storing information and instructions, or electrically erasable programmable read-only memory (EEPROM), compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but not limited thereto. The memory 902 may be integrated with the processor 901 or exist independently, and may be connected via the interface circuit of the dead loop detection device 900. Figure 9 (Not shown in the image) is coupled to the processor 901, but this embodiment does not specifically limit this.
[0321] Transceiver 903 is used for dead loop detection with other dead loop detection devices. For example, if dead loop detection device 900 is a terminal device, transceiver 903 can be used for dead loop detection with a network device or with another terminal device. As another example, if dead loop detection device 900 is a network device, transceiver 903 can be used for dead loop detection with a terminal device or with another network device.
[0322] Alternatively, transceiver 903 may include a receiver and a transmitter. Figure 9 (Not shown separately). The receiver is used to implement the receiving function, and the transmitter is used to implement the sending function.
[0323] Optionally, the transceiver 903 can be integrated with the processor 901, or it can exist independently and be connected to the interface circuit of the dead loop detection device 900. Figure 9 (Not shown in the image) is coupled to the processor 901, but this embodiment does not specifically limit this.
[0324] It should be noted that, Figure 9 The structure of the dead loop detection device 900 shown does not constitute a limitation on the dead loop detection device. An actual dead loop detection device may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0325] Furthermore, the technical effect of the dead loop detection device 900 can be referred to the technical effect of the dead loop detection method described in the above method embodiments, and will not be repeated here.
[0326] This application also provides a chip system, including: a processor coupled to a memory, the memory being used to store programs or instructions, wherein when the program or instructions are executed by the processor, the chip system implements the methods in any of the above method embodiments.
[0327] Optionally, the chip system may contain one or more processors. These processors can be implemented in hardware or software. When implemented in hardware, the processor can be a logic circuit, an integrated circuit, etc. When implemented in software, the processor can be a general-purpose processor, implemented by reading software code stored in memory.
[0328] Optionally, the chip system may contain one or more memories. The memory may be integrated with the processor or disposed separately from it; this application does not limit this. For example, the memory may be a non-transient processor, such as a read-only memory (ROM), which may be integrated with the processor on the same chip or disposed separately on different chips. This application does not specifically limit the type of memory or the arrangement of the memory and processor.
[0329] For example, the chip system may be a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), a system on chip (SoC), a central processor unit (CPU), a network processor (NP), a digital signal processor (DSP), a micro controller unit (MCU), a programmable logic device (PLD), or other integrated chips.
[0330] This application provides a dead loop detection system. The dead loop detection system includes one or more terminal devices and one or more network devices.
[0331] It should be understood that the processor in the embodiments of this application can be a central processing unit (CPU), or it can be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor or any conventional processor.
[0332] It should also be understood that the memory in the embodiments of this application can be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of random access memory (RAM) are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate synchronous DRAM (DDR SDRAM), enhanced synchronous DRAM (ESDRAM), synchronous linked DRAM (SLDRAM), and direct rambus RAM (DR RAM).
[0333] The above embodiments can be implemented, in whole or in part, by software, hardware (such as circuits), firmware, or any other combination thereof. When implemented using software, the above embodiments can be implemented, in whole or in part, in the form of a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more sets of available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium. A semiconductor medium can be a solid-state drive.
[0334] It should be understood that the term "and / or" in this article 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, or B alone. A and B can be singular or plural. Additionally, the character " / " in this article generally indicates an "or" relationship between the preceding and following related objects, but it can also represent an "and / or" relationship. Please refer to the context for a more accurate understanding.
[0335] In this application, "at least one" means one or more, and "more than one" means 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 multiple items. For example, at least one of a, b, or c can mean: a, b, c, ab, ac, bc, or abc, where a, b, and c can be single or multiple.
[0336] It should be understood that in the various embodiments of this application, the order of the above-mentioned processes does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0337] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software 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, but such implementation should not be considered beyond the scope of this application.
[0338] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0339] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of 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 system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0340] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0341] In addition, 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.
[0342] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) 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.
[0343] 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 that can be easily conceived by those skilled in the art within the scope of the technology 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 infinite loops, characterized in that, The method includes: Determine an intermediate representation of the application, wherein the intermediate representation is determined based on the application's source code and / or installation package, and the intermediate representation includes a linear intermediate representation and a graph intermediate representation; The loop body is obtained based on the intermediate representation; Determining that the loop body has a risk of entering an infinite loop includes: The linear intermediate representation includes the static single-assignment representation; the static single-assignment representation of the loop body is determined; if the second condition is met, it is determined that the loop body has a risk of entering an infinite loop, wherein the second condition includes: all variables included in the initial loop condition in the static single-assignment representation have been assigned values; or, The change amount of the loop body is determined, wherein the change amount is the difference between the symbolic expressions during two consecutive executions of the loop body; wherein, during the execution of the loop body, the unknown loop variable is represented by the first variable; if the third condition is met, it is determined that the loop body has the risk of entering an infinite loop, wherein the third condition is determined based on the first variable, the loop condition, the initial condition, and the change amount.
2. The dead loop detection method according to claim 1, characterized in that, The diagram in the middle includes a method call graph, which is used to represent the calling relationships between methods; The step of obtaining the loop body based on the intermediate representation includes: For each node in the method call graph, a depth-first search is performed to obtain strongly connected components, where each node represents a method; The loop body is determined based on the strongly connected components.
3. The dead loop detection method according to claim 1 or 2, characterized in that, The determination that the loop body has a risk of entering an infinite loop also includes: Determine the dependencies between the variables in the loop body; If the first condition is met, it is determined that the loop body is at risk of entering an infinite loop. The first condition is determined based on the dependency relationship and includes: the loop variable of the loop body is not assigned a value in the loop body.
4. The dead loop detection method according to claim 1, characterized in that, The third condition includes one of the following: There exists a value for the first variable such that the initial condition is satisfied, and the sign of the change is the same as the sign of the loop condition; wherein the sign includes one of the following: positive, non-negative, 0, non-positive, or negative; or, If there exists a value for the first variable such that the sign of the loop condition is not 0, then the initial condition is satisfied, where the sign of the initial condition is positive or negative, and the change is 0; or... There exist values for the first variable such that the sign of the loop condition includes both positive and negative, the sign of the initial condition is not 0, and the initial condition is a non-integer multiple of the change.
5. A dead loop detection device, characterized in that, The device includes: a determining module and an acquiring module; wherein... The determining module is used to determine the intermediate representation of the application, wherein the intermediate representation is determined based on the application's source code and / or installation package, and the intermediate representation includes linear intermediate representation and graph intermediate representation; The acquisition module is used to acquire the loop body based on the intermediate representation; The determining module is further configured to determine whether the loop body has a risk of entering an infinite loop, including: The linear intermediate representation includes the static single-assignment representation; The determining module is also used to determine the static single-assignment representation of the loop body; The determining module is further configured to determine that the second condition is met and to determine that the loop body has a risk of entering an infinite loop, wherein the second condition includes: all variables included in the initial loop condition in the static single assignment representation have been assigned values; or, The determining module is further configured to determine the change amount of the loop body, wherein the change amount is the difference between the symbolic expressions during two consecutive executions of the loop body; During the execution of the loop body, the unknown loop variable is represented by the first variable; The determining module is further configured to determine that the third condition is met and to determine that the loop body is at risk of entering an infinite loop, wherein the third condition is determined based on the first variable, the loop condition, the initial condition, and the change amount.
6. The dead loop detection device according to claim 5, characterized in that, The diagram in the middle includes a method call graph, which is used to represent the calling relationships between methods; The acquisition module is used to perform a depth-first search for each node in the method call graph to obtain strongly connected components, wherein each node represents a method; The acquisition module is further configured to determine the loop body based on the strongly connected components.
7. The dead loop detection device according to claim 5 or 6, characterized in that, The determining module is also used to determine the dependencies between the variables in the loop body; The determining module is further configured to determine that the first condition is met and to determine that the loop body has a risk of entering an infinite loop, wherein the first condition is determined according to the dependency relationship, and the first condition includes: the loop variable of the loop body is not assigned a value in the loop body.
8. The dead loop detection device according to claim 5, characterized in that, The third condition includes one of the following: There exists a value for the first variable such that the initial condition is satisfied, and the sign of the change is the same as the sign of the loop condition; wherein the sign includes one of the following: positive, non-negative, 0, non-positive, or negative; or, If there exists a value for the first variable such that the sign of the loop condition is not 0, then the initial condition is satisfied, where the sign of the initial condition is positive or negative, and the change is 0; or... There exist values for the first variable such that the sign of the loop condition includes both positive and negative, the sign of the initial condition is not 0, and the initial condition is a non-integer multiple of the change.
9. A dead loop detection device, characterized in that, The dead loop detection device is used to perform the dead loop detection method as described in any one of claims 1-4.
10. A dead loop detection device, characterized in that, The dead loop detection device includes: a processor; wherein... The processor is configured to execute the dead loop detection method as described in any one of claims 1-4.
11. A dead loop detection device, characterized in that, include: Processor, the processor being coupled to memory; The processor is configured to execute a computer program stored in the memory, so that the dead loop detection device performs the dead loop detection method as described in any one of claims 1-4.
12. A dead loop detection device, characterized in that, include: processor; The processor is configured to be coupled to a memory and, after reading instructions from the memory, execute the dead loop detection method as described in any one of claims 1-4 according to the instructions.
13. A dead loop detection device, characterized in that, include: Processor and memory; The memory is used to store computer instructions, which, when executed by the processor, cause the dead loop detection device to perform the dead loop detection method as described in any one of claims 1-4.
14. A dead loop detection device, characterized in that, include: Processor and interface circuits; among which, The interface circuit is used to receive code instructions and transmit them to the processor; The processor is used to run the code instructions to perform the method as described in any one of claims 1-4.
15. A dead loop detection device, characterized in that, The dead loop detection device includes a processor and a transceiver. The transceiver is used for information exchange between the dead loop detection device and other dead loop detection devices. The processor executes program instructions to perform the dead loop detection method as described in any one of claims 1-4.
16. A processor, characterized in that, include: The processor is used to execute the dead loop detection method as described in any one of claims 1-4.
17. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a computer program or instructions that, when executed on a computer, cause the computer to perform the dead loop detection method as described in any one of claims 1-4.
18. A computer program product, characterized in that, The computer program product includes: a computer program or instructions that, when run on a computer, cause the computer to perform the infinite loop detection method as described in any one of claims 1-4.