1 Modification to the current specifications.
4 -- Sequential order (SO): Some total order that is consistent with the union of
5 happens-before and SC relation.
8 Previously, we keep a sequential state throughout the process of executing the
9 sequential history. In our model, we keep a state local to each method call.
10 Conceptually, this state is the accumulative side effects of the subset of
11 method calls that happen before the current method call.
13 To evaluate the state of each method call, an obvious approach is to
14 execute the subset of methods that happen before the current method in the
15 sequential order from the initial state. A optimization we can make is that we
16 start to evaluate the state from the most recent deviding node which every other
17 node in that subset is either hb before or after.
20 Our specification language supports using the following primitives to access the
21 state of method calls so that users can use those to write specifications with
22 different level of tightness.
24 To support tighter specifications, we introduce the concept of concurrent set of
25 method calls, meaning that for a specific method call, it can basically see the
26 effect of two different categories of method calls --- one that happens before
27 it, and one that concurrently execute with it. It is worth noting that when two
28 two method calls execute concurrently, in general there can be the following two
29 effects: 1) those concurrent methods can happen in either order, and the final
30 result remains the same. A concurrent FIFO is an example, in which concurrent
31 enqueue and dequeue methods can happen in a random order; and 2) the order of
32 those concurrent methods will affect the final result. The C/C++11 atomics is an
33 example, in which when concurrent stores to the same location execute in
34 different order, a later store will have different result.
36 1. CONCURRENT: This primitive extracts all the methods that executes
37 "concurrently" with the current method --- neither hb/SC before nor after the
38 current method --- and returns as a set. It is worth noting that in order to
39 preserve composability, when evaluating the state, the concurrent method calls'
40 information cannot be used. That is to say, the concurrent set of mehtods are
41 only used for assertions.
43 2. PREV: This primitive extracts all the methods that execute right before the
44 current method in the execution graph --- most recent method calls that are
45 hb/SC before the current method call --- and returns as a set. For each method
46 in this set, the current method's specification can access their state.
48 3. NEXT: This primitive extracts all the methods that execute right after the
49 current method in the execution graph, and returns as a set. For each method in
50 this set, the current method's specification CANNOT access their state (for
51 preserving composability).
53 // FIXME: This might break composability!!??!!
54 4. FINAL: This is the function that allows users to specify some final check
55 on the state. This will enable to users to use the graph model (the relaxed
56 atomics can be specified) although the complxity of using that can get quite
59 Our specifications allow two ways of evaluating the state of method calls. One
60 way is to define "@Transition" on method calls, and then our checker executes
61 the method calls that are hb/SC before me in the sequential order starting from
62 the initial state. The other way is to define "@EvaluateState" after
63 "@Transition", which can only access the states of method calls that are hb/SC
64 before it. Usually users calculate the state by using the PREV primitive to
65 access the state of previous method calls.
69 /// Global specification
70 @State: // Declare the state structure
71 @Initial: // How do we initialize the state
72 @Commutativity: Method1 <-> Method2 (Guard) // Guard can only access the return
73 // value and the arguments of the two method calls
75 /// Interface specification
76 @Interface: InterfaceName // Required; a label to represent the interface
77 @Transition: // Optional; the transitional side effect from the initial state to
78 // the current method call by executing such effect on method calls
79 // that are hb/SC before me in sequential order. When this field is
80 // omitted, the current state seen before checking is the same as
82 @EvaluateState: // Optional; to calculate the state before this
83 // method call is checked in a customized fashion. This is
84 // evaluated after "@Transition". If omitted, the state we would
85 // see is the effect after the "@Transition".
86 @PreCondition: // Optional; checking code
87 @SideEffect: // Optional; to calculate the side effect this method call
88 // have on the state after the "@PreCondition".
89 @PostCondition: // Optional; checking code
92 /// Convienient operations
93 We define a struct called MethodCall to represent the data we would collect and
94 communicate between the real execution and the checking process.
97 STATE(field) // A primitive to access the current state in the interface
98 // specification by the name of the field. We can use this as a
99 // lvalue & rvalue ==> "STATE(x) = 1" and "int i = STATE(x)" are
102 // This can also be used to refer to the state of a method item
103 // in the Subset operation (see Subset)
105 NAME // Used to refer to the name of a method item in the Subset
106 // operation (see Subset)
108 RET(type) // Used to refer to the return value of a method item in the
109 // Subset operation (see Subset)
111 ARG(type, arg) // Used to refer to the argument of a method item in the
112 // Subset operation (see Subset)
115 PREV // Represent the set of previous method calls of the current method
118 CONCURRENT // Represent the set of concurrent method calls of the current
121 NEXT // Represent the set of next method calls of the current method
124 Name(method) // The interface label name of the specific method
126 State(method, field) // A primitive to access a specific state field of a
127 // specific method. Also used as lvalue and rvalue
129 Ret(method, type) // The return value of the specific method; type should
130 // be the name of the interface label
132 Arg(method, type, arg) // The arguement by name of the specific method; type should
133 // be the name of the interface label, and arg should be
134 // the name of the argument
136 Prev(method) // Represent the set of previous method calls of the
137 // specific method call
139 Next(method) // Represent the set of next method calls of the specific
142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
143 ForEach(item, container) { ... } // Useful iteration primitive
145 NewMethodSet // Create a new method set (set<MethodCall*>*)
147 MakeSet(NewSetType, oldset, newset, field); //
149 Subset(set, condition) // A generic subset operation that takes a condition and
150 // returns all the item for which the boolean guard
151 // holds. The condition can be specified with GUARD or
152 // GeneralGuard shown as follow.
154 ITEM // Used to refer to the set item in the GeneralGuard shown below
156 GeneralGuard(type, expression) // Used as a condition for any set<T> type. We
157 // use "ITEM" to refer to a set item. For
158 // example, a subset of positive elements on an
159 // IntSet "s" would be
160 // "Subset(s, GeneralGuard(int, ITEM > 0))"
162 Guard(expression) // Used specifically for MethodSet(set<MethodCall*>). An
163 // example to extract the subset of method calls in the PREV
164 // set that is a "Store" method and whose state "x" is equal
165 // to "val" and the return value is 5 would be
166 // "Subset(PREV, Guard(STATE(x) == val && NAME == "Store" &&
167 // RET(Store) == 5))"
169 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
173 To make things easier, we have the following helper macros.
174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
176 This is a common macro that is used as a constant for the name of specific
177 variables. We basically have two usage scenario:
178 1. In Subset operation, we allow users to specify a condition to extract a
179 subset. In that condition expression, we provide NAME, RET(type), ARG(type,
180 field) and LOCAL(field) to access each item's (method call) information.
181 2. In general specification (in pre- & post- conditions and side effects),
182 we would automatically generate an assignment that assign the current
183 MethodCall* pointer to a variable namedd _M. With this, when we access the
184 local state of the current method, we use LOCAL(field) (this is a reference
190 #define CAT(a, b) CAT_HELPER(a, b) /* Concatenate two symbols for macros! */
191 #define CAT_HELPER(a, b) a ## b
192 #define X(name) CAT(__##name, __LINE__) /* unique variable */
194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
196 *****************************************************************************
197 typedef struct MethodCall {
198 string name; // The interface label name
199 void *value; // The pointer that points to the struct that have the return
200 // value and the arguments
201 void *state; // The pointer that points to the struct that represents
203 set<MethodCall*> *prev; // Method calls that are hb right before me
204 set<MethodCall*> *concurrent; // Method calls that are concurrent with me
205 set<MethodCall*> *next; // Method calls that are hb right after me
208 typedef MethodCall *Method;
209 typedef set<Method> *MethodSet;
211 typedef vector<int> IntVector;
212 typedef list<int> IntList;
213 typedef set<int> IntSet;
214 *****************************************************************************
216 We will automatically generate two types of struct. One is the state struct, and
217 we will call it StateStruct. The other one is a per interface struct, and it
218 wraps the return value (RET) and the arguments as its field. We will name those
219 struct with the interface label name.
221 -----------------------------------------------------------------------------
222 For example, if we declare an ordered integer list in the specification state ,
223 we will generate a struct as follow.
225 @State: IntList *queue;
226 @Initial: queue = new IntList;
230 typedef struct StateStruct {
234 -----------------------------------------------------------------------------
235 If we have two interface whose specifications are as follow, we would generate
236 two struct accordingly.
240 void store(atomic_int *loc, int val) ...
244 int load(atomic_int *loc) ...
247 typedef struct Store {
252 typedef struct Load {
257 -----------------------------------------------------------------------------
258 We will put all these generated struct in a automatically-generated header file
259 called "generated.h". This file also includes header files that have commonly
260 used data types that interface (return value and arguments) accesses. In order
261 to make things work, users should include "generated.h" file in the end for
262 using our specification checker.
264 *****************************************************************************
267 // Some nice C/C++ macro definition to make specifications easier to write
269 -----------------------------------------------------------------------------
270 #define CAT(a, b) CAT_HELPER(a, b) /* Concatenate two symbols for macros! */
271 #define CAT_HELPER(a, b) a ## b
272 #define X(name) CAT(__##name, __LINE__) /* unique variable */
276 -----------------------------------------------------------------------------
277 1. ForEach --> to iterate a set, list or vector (containers that support using
280 #define ForEach(item, container) \
281 auto X(_container) = (container); \
282 auto X(iter) = X(_container)->begin(); \
283 for (auto item = *X(iter); X(iter) != X(_container)->end(); item = ((++X(iter)) != \
284 X(_container)->end()) ? *X(iter) : 0)
286 -----------------------------------------------------------------------------
289 This is a common macro that is used as a constant for the name of specific
290 variables. We basically have two usage scenario:
291 1. In Subset operation, we allow users to specify a condition to extract a
292 subset. In that condition expression, we provide NAME, RET(type), ARG(type,
293 field) and LOCAL(field) to access each item's (method call) information.
294 2. In general specification (in pre- & post- conditions and side effects),
295 we would automatically generate an assignment that assign the current
296 MethodCall* pointer to a variable namedd _M. With this, when we access the
297 local state of the current method, we use LOCAL(field) (this is a reference
304 -----------------------------------------------------------------------------
306 // 1.1 Subset(set, guard) --> to get a subset of method calls by a boolean
307 // expression; This takes advantage of C++11 std::function features and C macros.
309 // 1.2 Size(set) --> to get the size of a method set
310 #define Size(set) set->size()
312 // 1.3 Belong(set, method) --> whether method belongs to set
313 #define Belong(set, method) std::find(set->begin(), set->end(), method) != set->end()
315 // 1.4 Intersect(set1, set2) --> the intersection of two method sets
316 inline MethodSet Intersect(MethodSet set1, MethodSet set2) {
317 MethodSet res = NewSet;
325 // 1.5 Union(set1, set2) --> the union of two method sets
326 inline MethodSet Union(MethodSet set1, MethodSet set2) {
327 MethodSet res = NewSet(set1);
329 if (!Belong(set1, m))
335 // 1.6 Insert(set, method) --> add a method to the set
336 inline bool Insert(MethodSet set, Method m) {
345 // 1.7 Subtract(set1, set2) --> subtract set2 from set1
346 inline MethodSet Subtract(MethodSet set1, MethodSet set2) {
347 MethodSet res = NewSet;
349 if (!Belong(set2, m))
355 // 1.8 MakeSet(count, ...) --> Make a set from the arguments
358 // 2. Local(method, field)
359 #define Local(method, field) ((StateStruct*) method->localState)->field
361 // 3. Value(method, type, field)
362 #define Value(method, type, field) ((type*) method->value)->field
364 #define Ret(method, type) Value(method, type, RET)
366 #define Arg(method, type, arg) Value(method, type, arg)
370 #defien Lable(method) method->interfaceName
373 #define Prev(method) mehtod->prev
376 #define Next(method) mehtod->next
378 // 7. Concurrent(method)
379 #define Concurrent(method) mehtod->concurrent
382 /// Ordering point specification
383 @OPDefine: condition // If the specified condition satisfied, the atomic
384 // operation right before is an ordering point
386 @PotentialOP(Label): condition // If the specified condition satisfied, the
387 // atomic operation right before is a potential
388 // ordering point, and we label it with a tag
390 @OPCheck(Label): condition // If the specified condition satisfied, the
391 // potential ordering point defined earlier with the
392 // same tag becomes an ordering point
394 @OPClear: condition // If the specified condition satisfied, all the
395 // ordering points and potential ordering points will be
398 @OPClearDefine: condition // If the specified condition satisfied, all the
399 // ordering points and potential ordering points will
400 // be cleared, and the atomic operation right before
401 // becomes an ordering point. This is a syntax sugar
402 // as the combination of an "OPClear" and "OPDefine"
406 1. The register examples: Basically, we can think of registers as the cache on a
407 memory system. The effect of a load or store basically tells us what the current
408 value in the cache line is, and a load can read from values that can be
409 potentially in the cache --- either one of the concurrent store update the cache
410 or it inherites one of the the previous local state in the execution graph.
412 ---------- Interfae ----------
413 void init(atomic_int &loc, int initial);
414 int load(atomic_int &loc);
415 void store(atomic_int &loc, int val);
416 ---------- Interfae ----------
418 a. The SC atomics --- the classic linearizability approach
420 b. The RA (release/acquire) C/C++ atomics
421 // For RA atomics, a load must read its value from a store that happens before
423 ---------- Specification ----------
428 @SideEffect: LOCAL(x) = val;
429 void store(int *loc, int val);
433 Size(Subset(PREV, LOCAL(x) == RET)) > 0;
434 @SideEffect: LOCAL(x) = RET;
437 c. The C/C++ atomics (a slightly loose specification)
438 // Actually, any concurrent data structures that rely modification-order to be
439 // correct would not have a precicely tight specification under our model, and
440 // C/C++ relaxed atomics is an example. See the following read-read coherence
451 Our model cannot prevent such a case from happening. However, we can still have
452 a slightly loose specification which basically states that a load can read from
453 any store that either immediately happens before it or concurrently executes.
456 ---------- Specification ----------
461 @SideEffect: LOCAL(x) = val;
462 void store(int *loc, int val);
467 // Auto generated code
468 // MethodCall *ME = ((SomeTypeConversion) info)->method;
470 int count = Size(Subset(Prev, LOCAL(x) == RET))
471 + Size(Subset(CONCURRENT, NAME == "Store" && ARG(Store, val) == RET))
473 @SideEffect: LOCAL(x) = RET;
476 d. The C/C++ normal memory accesses
477 - Use the admissibility requirement, then the classic linearizability approach
478 on the admissible executions
480 2. The FIFO queue example.
481 ---------- Specification ----------
482 // A FIFO queue should have the following properties held:
483 // 1. The enq() methods should conflict
484 // 2. The deq() methods that succeed should conflict
485 // 3. Corresponding enq() and deq() methods should conflict
486 // 4. An enqueued item can be dequeued by at most once
487 // 5. A dequeued item must have a corresponding enqueued item
488 // 6. When a queue is NOT "empty" (users can tightly or loosely define
489 // emptiness), and there comes a deq() method, the deq() method should succeed
492 @DeclareVar: vector<int> *q;
493 @InitVar: q = new voctor<int>;
494 @Copy: New.q = new vector<int>(Old.q);
496 @Commutativity: Deq <-> Deq (!_M1.RET || !_M2.RET)
497 // The dequeuer doesn't dequeue from that enqueuer
498 @Commutativity: Enq <-> Deq (!_M2.RET || (_M2.RET && Enq.val != *Deq.res))
501 @SideEffect: q->push_back(val);
502 void enq(queue_t *q, int val);
506 // Check whether the queue is really empty
507 // Either the local state is an empty queue, or for all those remaining
508 // elements in the local queue, there should be some concurrent dequeuers to
511 // Local state is empty
512 if (Local(q)->size() == 0) return true;
513 // Otherwise check there must be other concurrent dequeuers
514 ForEach (item, Local(q)) {
515 // Check there's some concurrent dequeuer for this item
516 if (Size(Subset(CONCURRENT, NAME == "Deq" && RET(Deq) &&
517 *ARG(Deq, res) == item)) == 0) return false;
520 } else { // Check the global queue state
521 return q->back() == *res;
524 if (RET) q->pop_back();
525 bool deq(queue_t *q, int *res);
529 A good example to simulate a queue data structure is as follows.
530 Suppose we have a special structure
536 , and we have two interface on Q, read() and write(), where the write and read
537 method calls are synchronized by themselves, and they have to read and write the
538 x and y fields in turn.
558 ----------------------------------------------------------------------------------------
559 We also need to think about the non-ordered queue.
562 Combiming Data Structures ---
563 For example, a queue, a -hb->c, b -hb-> e.
566 enq(1) -> {1} - {$} // a
567 enq(2) -> {1, 2} - {$} // b
570 deq(1) -> {$} - {1} // c
571 deq($) -> {$} - {1} // d
573 // State before this method
574 JOIN ({1, 2} - {$}, {$} - {1}) = {2} - {1}
575 deq(2) -> {$} - {1, 2}