Static deadlock detection method and device for Rust multi-thread program
Through domain-sensitive inter-process pointer analysis and lock graph representation of Rust intermediate language MIR, the problem of false positives and false negatives in Rust program deadlock detection tools is solved, and accurate detection of deadlock types in Rust multi-threaded programs is achieved, especially Detection of double locks, conflicting locks and condition variable related deadlocks.
Patent Information
- Application Number
- CN202410545528.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-06
- Publication Date
- 2024-08-27
AI Technical Summary
Existing deadlock detection tools for Rust programs have problems with false positives and false negatives, and cannot effectively detect deadlocks related to double locks, conflicting locks, and condition variables in Rust.
By performing domain-sensitive inter-procedure pointer analysis on Rust's intermediate language MIR, we analyze the dependencies between variables, and use lock graphs to represent dependencies to identify potential deadlocks, including the combination of constraint graphs, rules and solvers, considering Rust's Ownership mechanism and life cycle rules, separate analysis of move keyword and smart pointer Arc.
It achieves accurate detection of main deadlock types in Rust multi-threaded programs, reduces false positives and false negatives, and improves detection accuracy.
Smart Images

Figure CN118550720A
Abstract
Description
Technical Field
[0001] The present application relates to the field of software programming technology, and in particular to a static deadlock detection method and device for Rust multi-threaded programs. Background Art
[0002] Deadlock is a common concurrency defect that can cause the program to fail to run normally or even cause the system to crash. Existing deadlock detection work is mainly aimed at multi-threaded programs, and the detection methods are mainly divided into static detection, dynamic detection, and dynamic and static combined detection methods. Static deadlock detection directly analyzes the program source code or intermediate code, and calculates the dependencies between locks through static analysis methods such as data flow analysis and pointer analysis. A common one is the detection method based on lock sets. The static analysis method can analyze various situations more comprehensively, but it will produce more false positives. Dynamic analysis is a method of detecting deadlocks by tracking the execution status and resource allocation of the program while the program is running. The dynamic analysis method is more efficient, but because it only analyzes the path of the program while it is running, it will cause more false positives.
[0003] Rust is an emerging programming language that focuses on building efficient and secure low-level software. It inherits the excellent runtime performance of the C language and solves security issues through powerful compile-time checks. The core security mechanism is the concept of ownership, which allows each value to have only one owner and releases the value at the end of the owner's life cycle. Through a series of rules, Rust extends this basic principle to ensure the safety of memory and threads.
[0004] However, although Rust has implemented strict security measures, the program may still have some security vulnerabilities. At the same time, the introduction of new language features has also brought new challenges to vulnerability detection. Previous deadlock detection tools for C\C++ and Java programs cannot be directly applied to Rust programs, and existing Rust program deadlock detection tools have many false positive and false negative problems. Summary of the invention
[0005] In order to solve the problems of many false positives and missed positives in existing Rust program deadlock detection tools, the present application provides a static deadlock detection method and device for Rust multi-threaded programs.
[0006] The embodiment of the present application is implemented as follows:
[0007] In a first aspect, the present application provides a static deadlock detection method for Rust multi-threaded programs, comprising:
[0008] Perform domain-sensitive inter-procedural pointer analysis on Rust's intermediate language MIR to obtain pointing information;
[0009] Based on the obtained pointing information, the dependency relationship between variables is analyzed, and the dependency relationship is represented by a lock graph to identify potential deadlocks;
[0010] Output the detection results.
[0011] In a possible implementation, performing domain-sensitive inter-procedural pointer analysis on the Rust intermediate language MIR to obtain pointing information further includes:
[0012] Get Rust's intermediate language MIR;
[0013] A domain-sensitive inter-procedural pointer analysis is performed on the intermediate language MIR.
[0014] In a possible implementation, in the domain-sensitive inter-procedural pointer analysis of the intermediate language MIR, the domain sensitivity means considering the impact of variables in different scopes of the program to more accurately capture the reference relationship of pointers.
[0015] In a possible implementation, the inter-procedural pointer analysis implements inter-procedural pointer analysis by analyzing function calls and closures;
[0016] In this process, the move keyword and smart pointer Arc are analyzed and processed separately by fully considering the Rust ownership mechanism and life cycle rules.
[0017] In a possible implementation, the pointer analysis includes:
[0018] Constraint graph: Use constraint graph to represent the directional relationship between variables;
[0019] Rules: Follow a set of rules to build the initial constraint graph;
[0020] Solver: Use the fixed point algorithm to process the initial constraint graph, add constraint relationships and update the orientation information.
[0021] In a possible implementation, analyzing the dependencies between variables based on the obtained pointing information, representing the dependencies with a lock graph, and identifying potential deadlocks further includes:
[0022] Based on the obtained pointing information, the system analyzes the dependencies between variables in the Rust program, including the dependencies between lock variables and between lock variables and condition variables;
[0023] The dependency relationship is used to construct a lock graph, in which nodes represent lock variables or signal signals, and edges represent the dependency relationship between variables, so as to realize the detection of double locks, conflicting locks, and deadlocks related to condition variables.
[0024] In a possible implementation, the constructing of a lock graph using the dependency relationship further includes alias analysis, a lock graph, and an extended lock graph.
[0025] In a second aspect, the present application provides a static deadlock detection device for Rust multi-threaded programs, comprising:
[0026] Pointer analysis module, used to perform domain-sensitive inter-procedural pointer analysis on Rust's intermediate language MIR to obtain pointing information;
[0027] A deadlock detection module, used to analyze the dependency relationship between variables based on the obtained directional information, and to represent the dependency relationship with a lock graph to identify potential deadlocks;
[0028] The result output module is used to output the detection results.
[0029] The technical solution provided by this application can at least achieve the following beneficial effects:
[0030] The static deadlock detection method for Rust multi-threaded programs provided by this application fully considers Rust's ownership mechanism and life cycle, as well as new features of the concurrency mechanism, and can detect the main deadlock types in Rust, including double locks, conflicting locks, and condition variable-related deadlocks. Compared with existing deadlock detection tools, the present invention has higher accuracy and reduces most false positives and false negatives. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0032] Figure 1 It is a flowchart of a static deadlock detection method and device for Rust multi-threaded programs shown in an exemplary embodiment of the present application;
[0033] Figure 2 is a structural schematic diagram of a deadlock detection tool framework shown in an exemplary embodiment of the present application;
[0034] Figure 3 is a flow chart of a deadlock detection tool output according to an exemplary embodiment of the present application;
[0035] Figure 4 is a schematic diagram of a constraint rule shown in an exemplary embodiment of the present application;
[0036] Figure 5is a schematic diagram of an initial constraint graph shown in an exemplary embodiment of the present application;
[0037] Figure 6 is a schematic diagram of conflicting locks and corresponding lock diagrams shown in an exemplary embodiment of the present application;
[0038] Figure 7 is a schematic diagram of a deadlock related to a conditional variable and a corresponding extended lock graph shown in an exemplary embodiment of the present application;
[0039] Figure 8 It is a structural diagram of a static deadlock detection device for Rust multi-threaded programs shown in an exemplary embodiment of the present application.
[0040] Reference numerals:
[0041] 1. Pointer analysis module; 2. Deadlock detection module; 3. Result output module. DETAILED DESCRIPTION
[0042] In order to make the purpose, implementation mode and advantages of the present application clearer, the exemplary implementation mode of the present application will be clearly and completely described below in conjunction with the drawings in the exemplary embodiments of the present application. Obviously, the described exemplary embodiments are only part of the embodiments of the present application, not all of the embodiments. It should be understood that the specific embodiments described here are only used to explain the present application and are not used to limit the present application.
[0043] It should be noted that the brief description of terms in this application is only for the convenience of understanding the embodiments described below, and is not intended to limit the embodiments of this application. Unless otherwise specified, these terms should be understood according to their common and usual meanings.
[0044] The terms "first", "second", "third", etc. in the specification and claims of this application and the above drawings are used to distinguish similar or similar objects or entities, and do not necessarily mean to limit a specific order or sequence, unless otherwise noted. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances.
[0045] The terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover but not exclude inclusion, for example, a product or device comprising a list of components is not necessarily limited to all the components expressly listed but may include other components not expressly listed or inherent to such product or device.
[0046] Before explaining the static deadlock detection method for Rust multi-threaded programs provided by the embodiment of the present application, the application scenario and implementation environment of the embodiment of the present application are first introduced.
[0047] Deadlock is a common concurrency defect that can cause the program to fail to run normally or even cause the system to crash. Existing deadlock detection work is mainly aimed at multi-threaded programs, and the detection methods are mainly divided into static detection, dynamic detection, and dynamic and static combined detection methods. Static deadlock detection directly analyzes the program source code or intermediate code, and calculates the dependencies between locks through static analysis methods such as data flow analysis and pointer analysis. A common one is the detection method based on lock sets. The static analysis method can analyze various situations more comprehensively, but it will produce more false positives. Dynamic analysis is a method of detecting deadlocks by tracking the execution status and resource allocation of the program while the program is running. The dynamic analysis method is more efficient, but because it only analyzes the path of the program while it is running, it will cause more false positives.
[0048] Rust is an emerging programming language that focuses on building efficient and secure low-level software. It inherits the excellent runtime performance of the C language and solves security issues through powerful compile-time checks. The core security mechanism is the concept of ownership, which allows each value to have only one owner and releases the value at the end of the owner's life cycle. Through a series of rules, Rust extends this basic principle to ensure the safety of memory and threads.
[0049] However, although Rust has implemented strict security measures, the program may still have some security vulnerabilities. At the same time, the introduction of new language features has also brought new challenges to vulnerability detection. Previous deadlock detection tools for C\C++ and Java programs cannot be directly applied to Rust programs, and existing Rust program deadlock detection tools have many false positive and false negative problems.
[0050] Based on this, the present application provides a static deadlock detection method and device for Rust multi-threaded programs, which aims to detect deadlock errors in programs, including double locks, conflicting locks, and deadlocks related to conditional variables. The method first performs pointer analysis, taking into full account the new features of Rust's ownership mechanism, life cycle, and concurrency mechanism. Through the obtained pointing information, the dependencies between variables are analyzed, and the lock graph is used to represent this dependency in order to identify potential deadlock situations. Based on this method, a corresponding deadlock detection tool is developed. Experimental results show that the present invention has higher accuracy and can effectively detect deadlock problems in Rust programs.
[0051] Next, the technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems will be described in detail through embodiments and in combination with the accompanying drawings. The embodiments may be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. Obviously, the described embodiments are part of the embodiments of the present application, not all of them.
[0052] Figure 1 It is a flowchart of a static deadlock detection method and device for Rust multi-threaded programs shown in an exemplary embodiment of the present application.
[0053] In an exemplary embodiment, Figure 1 As shown, a static deadlock detection method for Rust multi-threaded programs is provided. In this embodiment, the method may include the following steps:
[0054] Step 100: Perform domain-sensitive inter-procedural pointer analysis on Rust's intermediate language MIR to obtain pointing information.
[0055] Step 200: Based on the obtained directional information, the dependency relationship between variables is analyzed, and the dependency relationship is represented by a lock graph to identify potential deadlocks.
[0056] Step 300: Output the detection result.
[0057] Figure 2 is a structural diagram of a deadlock detection tool framework shown in an exemplary embodiment of the present application, Figure 3 is a flow chart of a deadlock detection tool output according to an exemplary embodiment of the present application. Figure 4 is a schematic diagram of a constraint rule shown in an exemplary embodiment of the present application, Figure 5 It is a schematic diagram of an initial constraint graph shown in an exemplary embodiment of the present application.
[0058] In one possible implementation, Figure 2 and 3 As shown, the specific implementation process of this method includes:
[0059] For Rust's intermediate language MIR, we then perform domain-sensitive inter-procedural pointer analysis. Domain sensitivity means considering the impact of variables in different scopes of the program to more accurately capture the reference relationship of pointers. Inter-procedural pointer analysis is achieved by analyzing function calls and closures. In this process, the move keyword and smart pointer Arc are analyzed and processed separately by fully considering Rust's ownership mechanism and life cycle rules.
[0060] The pointer analysis process is divided into three parts: constraint graph, rules, and solver.
[0061] 1) Constraint graph: Constraint graph is used to represent the pointing relationship between variables. The graph consists of a node set N and an edge set E. Node n = (p, γ) ∈ N, where p represents the position in the MIR, which represents a certain position in the memory, γ represents the function instance where p is located, and edge e = (n s ,n t ,τ)∈E represents the slave node n s Point to nt The directed edges of , the edge type is τ∈T, where T = {address, copy, load, store, field}.
[0062] 2) Rules: Follow a set of rules to build the initial constraint graph. The rules cover the patterns of assignment statements and function calls, where Figure 4 An overview of the main assignment and function call rules is given. In the process of traversing all instances, corresponding edges are added to the constraint graph according to these rules to handle assignments and function calls. When processing move keywords and smart pointer Arc related statements, additional processing is performed to reduce the size of the constraint graph, thereby reducing time overhead. Figure 5 is an example of constructing an initial constraint graph based on rules.
[0063] 3) Solver: In this part, the fixed point algorithm is used to process the initial constraint graph, add constraint relationships and update the pointing information.
[0064] Figure 6 is a schematic diagram of conflicting locks and corresponding lock diagrams shown in an exemplary embodiment of the present application, Figure 7 It is a schematic diagram of a deadlock related to a conditional variable and a corresponding extended lock graph shown in an exemplary embodiment of the present application.
[0065] Based on the obtained pointing information, the system analyzes the dependencies between variables in the Rust program, including the dependencies between lock variables and between lock variables and condition variables. It uses this information to build a lock graph, in which nodes represent lock variables or signal signals and edges represent the dependencies between variables. This allows the detection of deadlocks related to double locks, conflicting locks, and condition variables.
[0066] By analyzing variables of type MutexGuard, RwLockReadGuard, RwLockWriteGuard, and Condvar, three types of deadlocks are detected: double locks, conflicting locks, and deadlocks related to condition variables.
[0067] 1) Double lock: This problem usually occurs within a single thread. By analyzing the life cycle of MutexGuard, a lock graph is constructed to represent the lock dependency (the same applies to RwLockReadGuard and RwLockWriteGuard). If two MutexGuards are in the same thread, share the same type, and have overlapping life cycles, alias analysis is performed to detect whether there is a double lock problem. If there is an alias relationship, there will be a double lock error. The thread uses λ = (η sw ,η jn ,β) indicates,η sw ,η jnrepresents the life cycle of the thread, β represents the context information when the thread is created, and G = (n, θ, η, β, λ, Σ) is used to describe the Guard structure corresponding to the lock. n represents the corresponding constraint graph node, θ∈{MX,R,W} The type of the lock corresponds to MutexGuard, RwLockReadGuard and RwLockWriteGuard respectively, η represents the location where the variable is introduced, that is, the line number in the source code, β represents the current context information, λ represents the current thread, and Σ is the set of other currently surviving guard variables.
[0068] 2) Conflicting locks: This problem usually occurs during the concurrent execution of multiple threads. By analyzing the life cycle of the thread and the type of the lock, a lock graph is constructed to represent the dependency of the locks. If two locks are in the same thread and their life cycles overlap, but their types are different or there is no alias relationship, it means that there is a conflicting lock problem. The lock graph can be used to intuitively represent this process. LG = (Ω, Π) describes the lock graph structure, where Ω is the set of all lock graph nodes, each node is a G, π = (G1, G2, k) ∈ Π represents an edge in the lock graph, k ∈ {D, A}, D represents a directed edge from G1 to G2, indicating the dependency between them, that is, obtaining G2 while holding G1, A represents a bidirectional edge from G1 to G2, indicating that they are aliases, and each cycle in the lock graph represents a conflicting lock. Figure 6 is an example of conflicting locks, and the constructed lock graph.
[0069] 3) Deadlock related to condition variables: Deadlock related to condition variables belongs to communication deadlock, using WT / NT=(n cvar ,n lock ,η,β,λ,Σ) respectively describe the wait() and notify() statements, n cvar and n lock They represent the constraint graph nodes associated with the condition variables and locks respectively. The meanings of β, λ, and Σ are the same as those in G. In order to describe the dependency between the condition variables and the locks, the lock graph needs to be expanded. ELG = (Ω, Π) represents the expanded lock graph. Ω is the set of all nodes, and each node ω is G or signal S. nt , π=(ω1,ω2,k)∈Π represents an edge in the lock graph, k∈{D,A} has the same meaning as in LG. When constructing the extended lock graph, some embodiments of the present application always assume that WT applies S nt NT holds S nt ,Similarly, each cycle in the extended lock graph represents a conflicting lock.
[0070] Figure 7is an example of a deadlock related to condition variables, and the constructed extended lock graph. In addition, deadlock can also occur due to improper use of condition variables. This type of deadlock mainly involves lost notification signals and conditions that are never met. Pairing wait() statements with notify() statements and checking whether threads can execute concurrently helps to effectively detect deadlocks caused by missing notification signals. For the second case, some embodiments of the present application detect it by analyzing the Σ of WT / NT.
[0071] It should be understood that, although the various steps in the flowcharts involved in the above embodiments are displayed in sequence as indicated, these steps are not necessarily executed in the order indicated. Unless otherwise clearly stated in this document, there is no strict order restriction on the execution of these steps, and these steps can be executed in other orders. Moreover, at least part of the steps in the flowcharts involved in the above embodiments may include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be executed in turn or alternating with other steps or at least part of the steps or stages in other steps.
[0072] Corresponding to the aforementioned embodiment of the static deadlock detection method for Rust multi-threaded programs, adopting the same technical concept, the present application also provides an embodiment of a static deadlock detection device for Rust multi-threaded programs.
[0073] Figure 8 It is a structural diagram of a static deadlock detection device for Rust multi-threaded programs shown in an exemplary embodiment of the present application.
[0074] In an exemplary embodiment, Figure 8 As shown, the static deadlock detection device for Rust multi-threaded programs includes:
[0075] Pointer analysis module 1 is used to perform domain-sensitive inter-procedural pointer analysis on Rust's intermediate language MIR to obtain pointing information.
[0076] The deadlock detection module 2 is used to analyze the dependency relationship between variables based on the obtained directional information, and to represent the dependency relationship with a lock graph to identify potential deadlocks.
[0077] The result output module 3 is used to output the detection result.
[0078] For the specific limitations of the static deadlock detection device for Rust multi-threaded programs, please refer to the limitations of the static deadlock detection method for Rust multi-threaded programs in the above text, which will not be repeated here. The various modules in the above-mentioned static deadlock detection device for Rust multi-threaded programs can be fully or partially implemented by software, hardware and their combination. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.
[0079] It can be seen that some embodiments of the present application propose a static deadlock detection method and device for Rust programs, which can detect most deadlock errors in Rust programs, mainly including double locks, conflicting locks, and deadlocks related to conditional variables. First, pointer analysis is performed, taking into full account the new features of Rust's ownership mechanism, life cycle, and concurrency mechanism. Then, based on the obtained pointing information, the dependencies between variables are analyzed, and a lock graph is used to represent such dependencies to identify potential deadlock situations. Based on this method, some embodiments of the present application develop corresponding deadlock detection tools. Experimental results show that the method proposed in the present invention has higher accuracy.
[0080] The technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0081] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A static deadlock detection method for Rust multi-threaded programs, characterized in that: include: Perform domain-sensitive inter-procedural pointer analysis on Rust's intermediate language MIR to obtain pointing information; Based on the obtained pointing information, the dependency relationship between variables is analyzed, and the dependency relationship is represented by a lock graph to identify potential deadlocks; Output the detection results.
2. The static deadlock detection method for Rust multi-threaded programs according to claim 1, characterized in that: The performing domain-sensitive inter-procedural pointer analysis on the Rust intermediate language MIR to obtain pointing information further includes: Get Rust's intermediate language MIR; A domain-sensitive inter-procedural pointer analysis is performed on the intermediate language MIR.
3. The static deadlock detection method for Rust multi-threaded programs as claimed in claim 2, characterized in that: In the domain-sensitive inter-procedural pointer analysis of the intermediate language MIR, the domain sensitivity means considering the impact of variables in different scopes of the program to more accurately capture the reference relationship of pointers.
4. The static deadlock detection method for Rust multi-threaded programs according to claim 2, characterized in that: The inter-procedural pointer analysis implements inter-procedural pointer analysis by analyzing function calls and closures; In this process, the move keyword and smart pointer Arc are analyzed and processed separately by fully considering the Rust ownership mechanism and life cycle rules.
5. The static deadlock detection method for Rust multi-threaded programs as claimed in claim 2, characterized in that: The pointer analysis includes: Constraint graph: Use constraint graph to represent the directional relationship between variables; Rules: Follow a set of rules to build the initial constraint graph; Solver: Use the fixed point algorithm to process the initial constraint graph, add constraint relationships and update the orientation information.
6. The static deadlock detection method for Rust multi-threaded programs as claimed in claim 3, characterized in that: The step of analyzing the dependencies between variables based on the obtained pointing information, and representing the dependencies with a lock graph to identify potential deadlocks further includes: Based on the obtained pointing information, the system analyzes the dependencies between variables in the Rust program, including the dependencies between lock variables and between lock variables and condition variables; The dependency relationship is used to construct a lock graph, in which nodes represent lock variables or signal signals, and edges represent the dependency relationship between variables, so as to realize the detection of double locks, conflicting locks, and deadlocks related to condition variables.
7. The static deadlock detection method for Rust multi-threaded programs according to claim 6, characterized in that: The use of the dependency relationship to construct a lock graph further includes alias analysis, a lock graph, and an extended lock graph.
8. A static deadlock detection device for Rust multi-threaded programs, characterized in that: include: Pointer analysis module, used to perform domain-sensitive inter-procedural pointer analysis on Rust's intermediate language MIR to obtain pointing information; A deadlock detection module, used to analyze the dependency relationship between variables based on the obtained directional information, and to represent the dependency relationship with a lock graph to identify potential deadlocks; The result output module is used to output the detection results.
