C language program verification method and device based on C11 weak memory model
By verifying the sequential consistency, modification sequence consistency and read consistency of C language programs in the weak memory model of C/C++11, combined with the SMT solver, the detection problem when writing concurrent code is solved and the code is improved. reliability and performance.
Patent Information
- Application Number
- CN202311361548.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-19
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2043-10-19
AI Technical Summary
In the weak memory model environment of C/C++11, writing correct and efficient concurrent code is challenging, and it is difficult for existing technologies to effectively detect the behavior and errors of concurrent code.
Through the C language program verification method based on the C11 weak memory model, the consistency axiom is used to determine the order consistency, modification order consistency and read consistency constraints, combined with the SMT solver to verify the executability of the operation, and explore various aspects of concurrent code possible behaviors, predicting the behavior and errors of concurrent code.
It improves the effectiveness of C language program detection, can predict the behavior and errors of concurrent code, and enhances the reliability and performance of the code.
Smart Images

Figure CN117667103B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer program detection, and in particular to a C language program verification method and device based on a C11 weak memory model. Background Art
[0002] With the development of computer hardware and compilers, weak memory models were introduced, which led to a balance between usability and performance. C / C++11 is the first C / C++ standard to deal with concurrency, which introduces low-level atomic operations and weak memory models to support high-performance multi-threaded programming. However, it is very challenging to write correct and efficient concurrent code in such an environment, because weak memory models and a large number of thread interleavings may lead to unpredictable behaviors and errors. Therefore, how to effectively detect C / C++11 programs has become an urgent problem to be solved. Summary of the invention
[0003] In view of the problems existing in the prior art, the present invention provides a C language program verification method and device based on the C11 weak memory model.
[0004] In a first aspect, the present invention provides a C language program verification method based on the C11 weak memory model, comprising:
[0005] Determine, based on the consistency axioms enforced in the weak memory model currently corresponding to the C language program, a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation;
[0006] Based on the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation, verify the executability of the first operation, and obtain a verification result of the C language program;
[0007] Wherein, the first operation includes modifying a write operation read by any current read operation of the C language program into another write operation;
[0008] The sequential consistency constraint is used to constrain the program execution order of write operations and read operations in the C language program, the modification order consistency constraint is used to constrain the memory modification order of write operations at the same memory location in the C language program, and the read consistency constraint is used to constrain the memory modification order of write operations at different memory locations in the C language program.
[0009] Optionally, the determining, based on the consistency axioms enforced in the weak memory model currently corresponding to the C language program, the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation includes:
[0010] Determine a sequential consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation or a memory modification order relationship of a write operation in a weak memory model currently corresponding to the C language program; and
[0011] Determine a modification order consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation in a weak memory model currently corresponding to the C language program; and
[0012] Based on the happens-before relationship between the write operation and the read operation and the corresponding relationship between the write operation and the read operation in the weak memory model currently corresponding to the C language program, determine the read consistency constraint corresponding to the first operation.
[0013] Optionally, verifying the executability of the first operation based on a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation includes:
[0014] verifying the executability of the first operation based on the consistency constraint corresponding to the first operation, the synchronization relationship constraint corresponding to the first operation, and the data validity constraint corresponding to the first operation;
[0015] The consistency constraint corresponding to the first operation is determined based on a sequential consistency constraint, a modification order consistency constraint, and a read consistency constraint corresponding to the first operation.
[0016] Optionally, verifying the executability of the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation includes:
[0017] Determine an execution constraint corresponding to the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation;
[0018] Solving the execution constraint corresponding to the first operation based on a satisfiability modulo theory (SMT) solver;
[0019] Based on a result of solving the execution constraint corresponding to the first operation, the executability of the first operation is verified.
[0020] Optionally, the method further comprises:
[0021] Based on the SMT solver, check whether the execution constraint corresponding to the first operation has assertion violation, data competition or deadlock.
[0022] Optionally, the method further comprises:
[0023] When it is determined that the first operation is executable, the first operation is applied to the C language program, and the consistency axioms enforced in the weak memory model currently corresponding to the C language program are updated until the executable first operation cannot be determined.
[0024] In a second aspect, the present invention further provides a C language program detection device based on a weak memory model, comprising:
[0025] A determination module, configured to determine a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation based on a consistency axiom enforced in a weak memory model currently corresponding to the C language program;
[0026] A verification module, used to verify the executableness of the first operation based on the sequential consistency constraint, the modification sequential consistency constraint and the read consistency constraint corresponding to the first operation, and obtain a verification result of the C language program;
[0027] Wherein, the first operation includes modifying a write operation read by any current read operation of the C language program into another write operation;
[0028] The sequential consistency constraint is used to constrain the program execution order of write operations and read operations in the C language program, the modification order consistency constraint is used to constrain the memory modification order of write operations at the same memory location in the C language program, and the read consistency constraint is used to constrain the memory modification order of write operations at different memory locations in the C language program.
[0029] In a third aspect, the present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the C language program verification method based on the C11 weak memory model as described in the first aspect above is implemented.
[0030] In a fourth aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the C language program verification method based on the C11 weak memory model as described in the first aspect above.
[0031] In a fifth aspect, the present invention further provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned C language program verification methods based on the C11 weak memory model.
[0032] The C language program verification method and device based on the C11 weak memory model provided by the present invention explore and verify various possible behaviors of concurrent code by verifying the write operations that may be read by each load operation in the program through the consistency constraints in the weak memory model, thereby predicting the behavior and errors of concurrent code and improving the effectiveness of C language program detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces 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 invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0034] Figure 1 It is a flow chart of a C language program verification method based on the C11 weak memory model provided by the present invention;
[0035] Figure 2 It is an example diagram of the process flow of the consistency restriction enforced by the weak memory model provided by the present invention;
[0036] Figure 3 It is a structural example diagram of a C language program detection device based on a weak memory model provided by the present invention;
[0037] Figure 4 It is a structural example diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0038] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0039] In order to more clearly understand the technical solutions of the embodiments of the present invention, some technical contents related to the present invention are first introduced.
[0040] As computer hardware and compilers evolve, weak memory models are introduced, which leads to a trade-off between usability and performance. C / C++11 is the first C / C++ standard to deal with concurrency, which introduces low-level atomic operations and a weak memory model to support high-performance multithreaded programming. However, writing correct and efficient concurrent code in such an environment is very challenging, because the weak memory model and a large number of thread interleavings may lead to unpredictable behaviors and errors.
[0041] The CPPMEM tool is built directly from formal specifications, and its main goal is to allow researchers to explore the implications of memory models. It explores all possible modification orders and read-from relationships (a source of redundancy), and therefore must search a fairly large search space.
[0042] CDS Checker is a stateless model checking tool for C / C++ code. It exhaustively explores the behavior of concurrent code under the C / C++11 memory model. It uses the Dynamic Partial Order Reduction (DPOR) technique to reduce the explored state space by exploring only those executions whose visible behavior may differ from previously explored executions. During its state space exploration, DPOR determines points where it must explore program operations in multiple interleavings. Conflict points are recorded in a backtrace set so that in future executions, the exploration can return (or backtrack) to the recorded program points and try different thread interleavings.
[0043] CPPMEM is primarily designed to explain and help understand the behavior of short and simple code snippets under the C / C++11 memory model. It does not support fences and infinite loops, and lacks support for most of the C / C++ language, so it cannot be applied to real programs. It explores a lot of redundant modification orders and read-from relationships.
[0044] In order to solve the above problems, the present invention applies maximal causality reduction (MCR) technology to stateless model checking of C / C++11 programs. Using a constraint-based approach, the behavior of concurrent code under the C / C++11 memory model is systematically explored. Specifically, a unique maximal causal model is extracted from a given execution trace to obtain the maximum possible set of causally equivalent executions. In addition, the present invention can be executed in parallel during checking to reduce the cost of offline analysis at runtime.
[0045] Figure 1 The flowchart of the C language program verification method based on the C11 weak memory model provided by the present invention is as follows: Figure 1 As shown, the method comprises the following steps:
[0046] Step 100: Based on the consistency axioms enforced in the weak memory model currently corresponding to the C language program, determine the sequential consistency constraint, modification sequential consistency constraint, and read consistency constraint corresponding to the first operation.
[0047] Step 101: Based on the sequential consistency constraint, modification sequential consistency constraint, and read consistency constraint corresponding to the first operation, verify the executability of the first operation and obtain a verification result of the C language program.
[0048] The first operation includes modifying a write operation currently read by any read operation of the C language program into another write operation;
[0049] Sequential consistency constraints are used to constrain the program execution order of write operations and read operations in C language programs. Modification order consistency constraints are used to constrain the memory modification order of write operations at the same memory location in C language programs. Read consistency constraints are used to constrain the memory modification order of write operations at different memory locations in C language programs.
[0050] Specifically, the detection of the C language program can be a stateless model detection of the C language program, exploring and verifying various possible behaviors of the concurrent code. Verifying various possible behaviors of the concurrent code requires determining the constraints used for verification. Exploring various possible behaviors of the concurrent code means exploring the write operations that each load operation may read, and thus determining the constraints used for verification means determining the constraints that each load operation reads from the possible write operations.
[0051] Modifying the write operation read by any current read operation of the C language program to another write operation is called the first operation. Based on the consistency axioms enforced in the weak memory model currently corresponding to the C language program, the sequential consistency constraints, modification sequential consistency constraints and read consistency constraints corresponding to the first operation can be determined.
[0052] Sequential consistency constraints can be used to constrain the program execution order of write operations and read operations in C language programs. Modification order consistency constraints can be used to constrain the memory modification order of write operations at the same memory location in C language programs. Read consistency constraints can be used to constrain the memory modification order of write operations at different memory locations in C language programs.
[0053] After determining the sequential consistency constraints, modified sequential consistency constraints, and read consistency constraints corresponding to the first operation, it is possible to verify whether the first operation can be executed based on the sequential consistency constraints, modified sequential consistency constraints, and read consistency constraints corresponding to the first operation, and obtain the verification result of the C language program (whether the current operation is executable).
[0054] The C language program verification method based on the C11 weak memory model provided by the present invention can also be applied to C++ programs and other computer language programs implemented based on the weak memory model, which are not specifically limited here.
[0055] The C language program verification method based on the C11 weak memory model provided by the present invention explores and verifies various possible behaviors of concurrent code by verifying the write operations that may be read by each load operation in the program through the consistency constraints in the weak memory model, thereby predicting the behavior and errors of the concurrent code and improving the effectiveness of C language program detection.
[0056] Optionally, based on the consistency axioms enforced in the weak memory model currently corresponding to the C language program, determining the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation includes:
[0057] Determine a sequential consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation or a memory modification order relationship of a write operation in a weak memory model currently corresponding to the C language program; and
[0058] Determine a modification order consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation in a weak memory model currently corresponding to the C language program; and
[0059] Based on the happens-before relationship between the write operation and the read operation and the corresponding relationship between the write operation and the read operation in the weak memory model currently corresponding to the C language program, a read consistency constraint corresponding to the first operation is determined.
[0060] Specifically, the sequential consistency constraint corresponding to the first operation can be determined according to the happens-before relationship between the write operation and the read operation or the memory modification order relationship of the write operation in the weak memory model currently corresponding to the C language program.
[0061] In one implementation, the sequential consistency constraint corresponding to the first operation may be determined according to the following formula:
[0062]
[0063] Among them, Φ sc represents the sequential consistency constraint corresponding to the first operation, SC represents the total order with sequential memory order and lock / unlock operations enforced on the weak memory model currently corresponding to the C language program, a and b are any write operation or any read operation in the weak memory model currently corresponding to the C language program, B ab Indicates the happens-before relationship between a and b, B ab ==1 means a occurs before b. a represents the sequential consistency order of a (i.e., the program execution order), S brepresents the sequential consistency order of b. x represents the memory address to be written, L represents the set of all shared memory addresses, and W x represents the set of write operations on x, M a Indicates the modification order of a (i.e., memory modification order), M b Indicates the modification order of b.
[0064] The modification order consistency constraint corresponding to the first operation can be determined according to the happens-before relationship between the write operation and the read operation in the weak memory model currently corresponding to the C language program.
[0065] In one implementation, the modification order consistency constraint corresponding to the first operation may be determined according to the following formula:
[0066]
[0067] Among them, Φ mo represents the modification order consistency constraint corresponding to the first operation, x represents the memory address to be written, L represents the set of all shared memory addresses, and W x represents the set of write operations performed on x, B ab Indicates the happens-before relationship between a and b, B ab ==1 means a happens before b, M a Indicates the modification order of a (i.e., memory modification order), M b Indicates the modification order of b.
[0068] The read consistency constraint corresponding to the first operation can be determined according to the happens-before relationship between the write operation and the read operation and the corresponding relationship between the write operation and the read operation in the weak memory model currently corresponding to the C language program.
[0069] In one implementation, the read consistency constraint corresponding to the first operation may be determined according to the following formula:
[0070] Φ rf =Φ corr ∧Φ corw ∧Φ cowr ∧Φ coww
[0071]
[0072]
[0073]
[0074]
[0075] Among them, Φrf represents the read consistency constraint corresponding to the first operation, Φ corr , Φ corw , Φ cowr and Φ coww All represent the consistency constraints enforced in the weak memory model, x represents the memory address to be written, L represents the set of all shared memory addresses, and W x represents the set of write operations performed on x, R x represents the set of read operations on x, F ab represents the correspondence between write operation a and read operation b, F ab ==1 means that the read operation b reads the data written by the write operation a, B ab Indicates the happens-before relationship between a and b, B ab ==1 means a happens before b, M a Indicates the modification order of a (i.e., memory modification order), M b Indicates the modification order of b.
[0076] Optionally, verifying the executability of the first operation based on a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation includes:
[0077] Verifying the executability of the first operation based on the consistency constraint corresponding to the first operation, the synchronization relationship constraint corresponding to the first operation, and the data validity constraint corresponding to the first operation;
[0078] The consistency constraint corresponding to the first operation is determined based on the order consistency constraint, the modification order consistency constraint, and the read consistency constraint corresponding to the first operation.
[0079] Specifically, after determining the consistency constraint corresponding to the first operation, it is also necessary to verify the executability of the first operation in combination with the synchronization relationship constraint corresponding to the first operation and the data validity constraint corresponding to the first operation.
[0080] The synchronization relationship constraint corresponding to the first operation and the data validity constraint corresponding to the first operation may be determined according to the synchronization relationship constraint and the data validity constraint in the existing determination procedure.
[0081] The consistency constraint corresponding to the first operation may be determined based on a sequential consistency constraint, a modification order consistency constraint, and a read consistency constraint corresponding to the first operation.
[0082] In one implementation, the consistency constraint corresponding to the first operation may be determined according to the following formula:
[0083] Φ co =Φ sc∧Φ mo ∧Φ rf
[0084] Among them, Φ co represents the consistency constraint corresponding to the first operation, Φ sc represents the sequential consistency constraint corresponding to the first operation, Φ mo represents the modification order consistency constraint, Φ rf Indicates the read consistency constraint corresponding to the first operation.
[0085] Optionally, verifying the executability of the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation includes:
[0086] Determine an execution constraint corresponding to the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation;
[0087] Solving the execution constraints corresponding to the first operation based on the Satisfiability Modulo Theories (SMT) solver;
[0088] Based on a result of solving the execution constraint corresponding to the first operation, the executability of the first operation is verified.
[0089] Specifically, based on the consistency constraint corresponding to the first operation, the synchronization relationship constraint corresponding to the first operation, and the data validity constraint corresponding to the first operation, the execution constraint corresponding to the first operation can be determined.
[0090] In one implementation, the execution constraint corresponding to the first operation may be determined according to the following formula:
[0091] Φ=Φ sync ∧Φ rw ∧Φ co
[0092] Among them, Φ represents the execution constraint corresponding to the first operation, Φ sync represents the synchronization constraint corresponding to the first operation, Φ rw represents the data validity constraint corresponding to the first operation, Φ co Indicates the consistency constraint corresponding to the first operation.
[0093] After determining the consistency constraint corresponding to the first operation, an SMT solver may be used to solve the execution constraint corresponding to the first operation, and then based on the result of solving the execution constraint corresponding to the first operation, the executableness of the first operation may be verified.
[0094] Optionally, the method further comprises:
[0095] Based on the SMT solver, check whether the execution constraint corresponding to the first operation has assertion violation, data competition or deadlock.
[0096] Specifically, the existing method for verifying assertion violations, data races or deadlocks can be used to check whether the execution constraints corresponding to the first operation have assertion violations, data races or deadlocks. This checking step is independent of the step of verifying the executable nature of the first operation, and the present invention does not specifically limit the execution order between the two.
[0097] Optionally, the method further comprises:
[0098] When it is determined that the first operation is executable, the first operation is applied to the C language program, and the consistency axioms enforced in the weak memory model currently corresponding to the C language program are updated until the executable first operation cannot be determined.
[0099] Specifically, after determining that the first operation is executable, the first operation can be applied to the C language program, and then the consistency axioms enforced in the weak memory model currently corresponding to the C language program are updated according to the first operation, and then the weak memory model corresponding to the updated C language program is verified and checked again until the executable first operation cannot be determined.
[0100] The method provided by the present invention is illustrated below by using specific application scenarios.
[0101] The C language program verification method based on the C11 weak memory model provided by the present invention can be used to perform stateless model detection on C / C++11 programs, and systematically explore and verify various possible behaviors of concurrent codes.
[0102] The overall algorithm of the C language program verification method based on the C11 weak memory model provided by the present invention is as follows:
[0103]
[0104] The algorithm mainly includes the following steps:
[0105] 1. Initialization: This algorithm initializes the workList (task list), which contains the seed pair <sch,M rf >, where sch is the random schedule, M rf is an empty map that imposes no restrictions.
[0106] 2. Iterative exploration: Start an iterative loop. In each loop, pop a seed pair from workList. <sch,M rf>, represents a specific type of execution (corresponding to step 5 in the algorithm).
[0107] 3. Scheduling execution: based on pop-up seed pairs <sch,M rf >, execute the program to simulate this specific execution, considering the synchronization operations between threads (corresponding to step 6 in the algorithm).
[0108] 4. Constraint construction: For each loading operation, this method constructs the constraint Φ rw , describing the constraints on the load operation reading from possible write operations, and the Φ between memory operations co and Φ sync Constraints (corresponding to step 7 in the algorithm).
[0109] 5. Property check: If the constraint Φ = Φ sync ∧Φ rw ∧Φ co The solution is satisfiable. Check whether the given property φ holds under the constraint Φ, so as to find a specific execution that satisfies the property (corresponding to step 8 in the algorithm).
[0110] 6. Generate new seeds: Generate a series of new seed pairs based on the current execution <sch,M rf > to explore new unexplored behaviors and add new seeds to newList (corresponding to step 9 in the algorithm).
[0111] 7. Iterative update: Add new seeds in newList to workList to continue exploring unknown execution paths (corresponding to step 10 in the algorithm).
[0112] 8. Termination condition: It terminates when no new seeds can be generated and exploration is required, indicating that all possible execution paths have been explored.
[0113] Through the above steps, the present invention can systematically and efficiently explore various possible behaviors of C / C++11 programs and verify whether given properties hold under these behaviors.
[0114] In order to systematically explore all possible behaviors, the present invention generates new seed pairs <sch,M rf >, thereby triggering new behaviors: when the execution is consistent, at least one read operation reads a different value. Considering all possible values for each read, it is guaranteed not to miss any unexplored behaviors, and eventually covers the entire state space. In this way, various possible behaviors of concurrent programs can be efficiently explored and verified.
[0115] The following is an algorithm for generating new seed pairs provided by the present invention:
[0116]
[0117] The algorithm generates new seed pairs by exploring the feasibility of reading different values for any read operation while keeping the current execution consistent.
[0118] Unlike many other model checking algorithms that are difficult to parallelize (such as DPOR and ICB), the detection algorithm provided by the present invention can be easily parallelized and achieves large-scale parallelism in two ways. First, the present invention can parallelize the process of generating different seed pairs. Second, the present invention can perform path exploration on different seed pairs in parallel because each seed pair is independent of each other.
[0119] The following is an algorithm for implementing parallel detection in the present invention:
[0120]
[0121] The algorithm achieves parallel testing of models by parallel verification of executable seed pairs.
[0122] The following consistency constraint Φ is provided by the present invention: co The encoding process is introduced.
[0123] The present invention encodes the consistency axioms enforced by the C / C++11 memory model as constraints Φ co to ensure consistent execution.
[0124] 1. Sequential consistency constraints.
[0125] The C / C++11 memory model enforces a total order on all atomic operations with sequential memory ordering and lock / unlock operations, denoted as the set SC. Let S a is the sequential consistency order of operation a. It can encode Φ sc To restrict the happens-before relationship in SC and modify the order relationship to SC order. sc The encoding is as follows:
[0126]
[0127] Among them, Φ sc represents a sequential consistency constraint, SC represents the total order of sequential memory order and lock / unlock operations enforced on the weak memory model currently corresponding to the C language program, a and b are any write operation or any read operation in the weak memory model currently corresponding to the C language program, B ab Indicates the happens-before relationship between a and b, B ab ==1 means a occurs before b. a represents the sequential consistency order of a (i.e., the program execution order), S brepresents the sequential consistency order of b. x represents the memory address to be written, L represents the set of all shared memory addresses, and W x represents the set of write operations on x, M a Indicates the modification order of a (i.e., memory modification order), M b Indicates the modification order of b.
[0128] 2. Modify the sequential consistency constraint.
[0129] The modification order is a strict total order of write operations to the same location, so the happens-before relationship should be restricted to the modification order relationship of the same location. Let M a is the modification order of operation a. The modification order is not comparable between different positions. Φ mo The encoding is as follows:
[0130]
[0131] Among them, Φ mo represents the modification order consistency constraint, x represents the memory address to be written, L represents the set of all shared memory addresses, and W x represents the set of write operations performed on x, B ab Indicates the happens-before relationship between a and b, B ab ==1 means a happens before b, M a Indicates the modification order of a (i.e., memory modification order), M b Indicates the modification order of b.
[0132] 3. Read-from consistency constraint.
[0133] The memory model places key restrictions on the values that can be read by each memory operation. The C / C++11 memory model enforces four types of consistency restrictions (CoRR, CoRW, CoWR, CoWW). Each restriction explains the prohibited behavior of the execution.
[0134] Figure 3 An example flow chart of the consistency restriction enforced by the weak memory model provided by the present invention is as follows: Figure 3 As shown, according to the happens-before relationship between write operations and read operations in the weak memory model and the corresponding relationship between write operations and read operations, the Read-from consistency constraint can be determined.
[0135] Φ rf By connecting four types of restrictions, Φ rf =Φ corr ∧Φ corw ∧Φ cowr∧Φ coww .
[0136] in,
[0137]
[0138]
[0139]
[0140]
[0141] Among them, Φ corr , Φ corw , Φ cowr and Φ coww All represent the consistency constraints enforced in the weak memory model, x represents the memory address to be written, L represents the set of all shared memory addresses, and W x represents the set of write operations performed on x, R x represents the set of read operations on x, F ab represents the correspondence between write operation a and read operation b, F ab ==1 means that the read operation b reads the data written by the write operation a, B ab Indicates the happens-before relationship between a and b, B ab ==1 means a happens before b, M a Indicates the modification order of a (i.e., memory modification order), M b Indicates the modification order of b.
[0142] The present invention uses the Maximal Causality Model (MCR) to describe redundant thread scheduling, thereby only exploring thread scheduling that reaches different program states. In other words, MCR will not probe the same program state multiple times under a given input. Therefore, under a given input, a provable minimum number of program executions to probe all program states can be guaranteed.
[0143] The program detection method provided by the present invention can be easily parallelized, can generate different seed pairs in parallel, and can perform path exploration on different seed pairs in parallel, because each seed pair is independent of each other.
[0144] The program detection method provided by the present invention can systematically and efficiently explore various possible behaviors of C / C++11 programs and verify whether given properties hold under these behaviors.
[0145] The following is a description of a C language program detection device based on a weak memory model provided by the present invention. The C language program detection device based on a weak memory model described below and the C language program verification method based on a C11 weak memory model described above can refer to each other.
[0146] Figure 3 The structure example diagram of the C language program detection device based on the weak memory model provided by the present invention is as follows: Figure 3 As shown, the device comprises:
[0147] A determination module 300, configured to determine a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation based on a consistency axiom enforced in a weak memory model currently corresponding to the C language program;
[0148] A verification module 310, configured to verify the executability of the first operation based on the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation, and obtain a verification result of the C language program;
[0149] The first operation includes modifying a write operation currently read by any read operation of the C language program into another write operation;
[0150] Sequential consistency constraints are used to constrain the program execution order of write operations and read operations in C language programs. Modification order consistency constraints are used to constrain the memory modification order of write operations at the same memory location in C language programs. Read consistency constraints are used to constrain the memory modification order of write operations at different memory locations in C language programs.
[0151] Optionally, based on the consistency axioms enforced in the weak memory model currently corresponding to the C language program, determining the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation includes:
[0152] Determine a sequential consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation or a memory modification order relationship of a write operation in a weak memory model currently corresponding to the C language program; and
[0153] Determine a modification order consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation in a weak memory model currently corresponding to the C language program; and
[0154] Based on the happens-before relationship between the write operation and the read operation and the corresponding relationship between the write operation and the read operation in the weak memory model currently corresponding to the C language program, a read consistency constraint corresponding to the first operation is determined.
[0155] Optionally, verifying the executability of the first operation based on a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation includes:
[0156] Verifying the executability of the first operation based on the consistency constraint corresponding to the first operation, the synchronization relationship constraint corresponding to the first operation, and the data validity constraint corresponding to the first operation;
[0157] The consistency constraint corresponding to the first operation is determined based on the order consistency constraint, the modification order consistency constraint, and the read consistency constraint corresponding to the first operation.
[0158] Optionally, verifying the executability of the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation includes:
[0159] Determine an execution constraint corresponding to the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation;
[0160] Solve the execution constraint corresponding to the first operation based on the satisfiability modulo theory SMT solver;
[0161] Based on a result of solving the execution constraint corresponding to the first operation, the executability of the first operation is verified.
[0162] Optionally, the device further comprises:
[0163] The checking module is used to check whether the execution constraint corresponding to the first operation has assertion violation, data competition or deadlock based on the SMT solver.
[0164] Optionally, the device further comprises:
[0165] The application module is used to apply the first operation to the C language program when it is determined that the first operation is executable, and update the consistency axioms enforced in the weak memory model currently corresponding to the C language program until the executable first operation cannot be determined.
[0166] It should be noted here that the above-mentioned device provided by the present invention can implement all the method steps implemented by the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects that are the same as the method embodiment in this embodiment will not be described in detail here.
[0167] Figure 4 The structure of the electronic device provided by the present invention is shown as follows: Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute any of the C language program verification methods based on the C11 weak memory model provided in the above embodiments.
[0168] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0169] It should be noted here that the electronic device provided by the present invention can implement all the method steps implemented by the above-mentioned method embodiment, and can achieve the same technical effects. The parts and beneficial effects of this embodiment that are the same as the method embodiment will not be described in detail here.
[0170] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute any of the C language program verification methods based on the C11 weak memory model provided in the above embodiments.
[0171] It should be noted here that the non-transitory computer-readable storage medium provided by the present invention can implement all the method steps implemented by the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects that are the same as the method embodiment in this embodiment will not be described in detail here.
[0172] On the other hand, the present invention also provides a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute any of the C language program verification methods based on the C11 weak memory model provided in the above embodiments.
[0173] It should be noted here that the computer program product provided by the present invention can implement all the method steps implemented by the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects of this embodiment that are the same as those of the method embodiment will not be described in detail here.
[0174] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0175] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0176] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A C language program verification method based on C11 weak memory model, It is characterized in that include: Determine, based on the consistency axioms enforced in the weak memory model currently corresponding to the C language program, a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation; Based on the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation, verify the executability of the first operation, and obtain a verification result of the C language program; Wherein, the first operation includes modifying a write operation read by any current read operation of the C language program into another write operation; The sequential consistency constraint is used to constrain the program execution order of write operations and read operations in the C language program, the modification order consistency constraint is used to constrain the memory modification order of write operations at the same memory location in the C language program, and the read consistency constraint is used to constrain the memory modification order of write operations at different memory locations in the C language program; The verifying the executability of the first operation based on the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation includes: Verifying the executability of the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation; The consistency constraint corresponding to the first operation is determined based on the order consistency constraint, the modification order consistency constraint and the read consistency constraint corresponding to the first operation; The verifying the executability of the first operation based on the consistency constraint corresponding to the first operation, the synchronization relationship constraint corresponding to the first operation, and the data validity constraint corresponding to the first operation includes: Determine an execution constraint corresponding to the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation; Solving the execution constraint corresponding to the first operation based on a satisfiability modulo theory (SMT) solver; Based on a result of solving the execution constraint corresponding to the first operation, the executability of the first operation is verified.
2. The C language program verification method based on the C11 weak memory model according to claim 1, It is characterized in that The determining of the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation based on the consistency axiom enforced in the weak memory model currently corresponding to the C language program includes: Determine a sequential consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation or a memory modification order relationship of a write operation in a weak memory model currently corresponding to the C language program; and Determine a modification order consistency constraint corresponding to the first operation based on a happens-before relationship between a write operation and a read operation in a weak memory model currently corresponding to the C language program; and Based on the happens-before relationship between the write operation and the read operation and the corresponding relationship between the write operation and the read operation in the weak memory model currently corresponding to the C language program, determine the read consistency constraint corresponding to the first operation.
3. The C language program verification method based on the C11 weak memory model according to claim 1, It is characterized in that The method further comprises: Based on the SMT solver, check whether the execution constraint corresponding to the first operation has assertion violation, data competition or deadlock.
4. The C language program verification method based on the C11 weak memory model according to any one of claims 1 to 3, It is characterized in that The method further comprises: When it is determined that the first operation is executable, the first operation is applied to the C language program, and the consistency axioms enforced in the weak memory model currently corresponding to the C language program are updated until the executable first operation cannot be determined.
5. A C language program detection device based on a weak memory model, It is characterized in that include: A determination module, configured to determine a sequential consistency constraint, a modification sequential consistency constraint, and a read consistency constraint corresponding to the first operation based on a consistency axiom enforced in a weak memory model currently corresponding to the C language program; A verification module, used to verify the executableness of the first operation based on the sequential consistency constraint, the modification sequential consistency constraint and the read consistency constraint corresponding to the first operation, and obtain a verification result of the C language program; Wherein, the first operation includes modifying a write operation read by any current read operation of the C language program into another write operation; The sequential consistency constraint is used to constrain the program execution order of write operations and read operations in the C language program, the modification order consistency constraint is used to constrain the memory modification order of write operations at the same memory location in the C language program, and the read consistency constraint is used to constrain the memory modification order of write operations at different memory locations in the C language program; The verifying the executability of the first operation based on the sequential consistency constraint, the modification sequential consistency constraint, and the read consistency constraint corresponding to the first operation includes: Verifying the executability of the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation; The consistency constraint corresponding to the first operation is determined based on the order consistency constraint, the modification order consistency constraint and the read consistency constraint corresponding to the first operation; The verifying the executability of the first operation based on the consistency constraint corresponding to the first operation, the synchronization relationship constraint corresponding to the first operation, and the data validity constraint corresponding to the first operation includes: Determine an execution constraint corresponding to the first operation based on a consistency constraint corresponding to the first operation, a synchronization relationship constraint corresponding to the first operation, and a data validity constraint corresponding to the first operation; Solving the execution constraint corresponding to the first operation based on a satisfiability modulo theory (SMT) solver; Based on a result of solving the execution constraint corresponding to the first operation, the executability of the first operation is verified.
6. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, It is characterized in that When the processor executes the program, the C language program verification method based on the C11 weak memory model as described in any one of claims 1 to 4 is implemented.
7. A non-transitory computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the C language program verification method based on the C11 weak memory model as claimed in any one of claims 1 to 4 is implemented.
8. A computer program product comprising a computer program, It is characterized in that When the computer program is executed by a processor, the C language program verification method based on the C11 weak memory model as claimed in any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Method for verifying compiler by graph isomorphism
CN103559125A
Modeling and verification of concurrent systems using SMT-based bmc
US20080281563A1
