1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Lincheck is a Rust library for testing concurrent data structures for [linearizability](https://en.wikipedia.org/wiki/Linearizability). Simply put, it checks whether a concurrent data structure behaves similarly to a simpler sequential implementation. It is inspired by [Lincheck for Kotlin](https://github.com/JetBrains/lincheck) and is built on top of [loom](https://github.com/tokio-rs/loom), a model-checker for concurrency.
//!
//! # Features
//!
//! - Lincheck uses [proptest](https://docs.rs/proptest/latest/proptest/) to generate random concurrent scenarios and automatically shrink them to a minimal failing scenario.
//! - Lincheck runs every scenario inside [loom](https://github.com/tokio-rs/loom) model-checker to check every possible interleaving of operations.
//! - Lincheck provides a simple API for defining concurrent data structures and their sequential counterparts.
//! - Recording of execution traces is made to introduce as little additional synchronization between threads as possible.
//!
//! # Tutorial
//!
//! ```rust
//! use lincheck::{ConcurrentSpec, Lincheck, SequentialSpec};
//! use loom::sync::atomic::{AtomicBool, Ordering};
//! use proptest::prelude::*;
//!
//! // Let's implement a simple concurrent data structure:
//! // a pair of boolean flags `x` and `y`
//! // that can be read and written by multiple threads.
//! // The flags are initialized to `false` and can be switched to `true`.
//!
//! // We start by defining the operations:
//! #[derive(Debug, Clone, Copy, PartialEq, Eq)]
//! enum Op {
//! WriteX, // set x to true
//! WriteY, // set y to true
//! ReadX, // get the value of x
//! ReadY, // get the value of y
//! }
//!
//! // ... and their results:
//! #[derive(Debug, Clone, Copy, PartialEq, Eq)]
//! enum Ret {
//! Write, // the result of a write operation
//! Read(bool), // the result of a read operation
//! }
//!
//! // We need to implement the `Arbitrary` trait for our operations
//! // to be able to generate them randomly:
//! impl Arbitrary for Op {
//! type Parameters = ();
//! type Strategy = BoxedStrategy<Self>;
//!
//! fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
//! prop_oneof![
//! Just(Op::WriteX),
//! Just(Op::WriteY),
//! Just(Op::ReadX),
//! Just(Op::ReadY),
//! ]
//! .boxed()
//! }
//! }
//!
//! // We then define the concurrent implementation that we want to test:
//! struct TwoSlotsParallel {
//! x: AtomicBool,
//! y: AtomicBool,
//! }
//!
//! // We implement the `ConcurrentSpec` trait for our implementation:
//! impl ConcurrentSpec for TwoSlotsParallel {
//! type Op = Op;
//! type Ret = Ret;
//!
//! // We must be able to create a new instance of our implementation:
//! fn new() -> Self {
//! Self {
//! x: AtomicBool::new(false),
//! y: AtomicBool::new(false),
//! }
//! }
//!
//! // We must be able to execute an operation on our implementation:
//! fn exec(&self, op: Op) -> Ret {
//! match op {
//! Op::WriteX => {
//! self.x.store(true, Ordering::Relaxed);
//! Ret::Write
//! }
//! Op::WriteY => {
//! self.y.store(true, Ordering::Relaxed);
//! Ret::Write
//! }
//! Op::ReadX => Ret::Read(self.x.load(Ordering::Relaxed)),
//! Op::ReadY => Ret::Read(self.y.load(Ordering::Relaxed)),
//! }
//! }
//! }
//!
//! // We then define the sequential implementation which we test against:
//! struct TwoSlotsSequential {
//! x: bool,
//! y: bool,
//! }
//!
//! // We implement the `SequentialSpec` trait for our implementation:
//! impl SequentialSpec for TwoSlotsSequential {
//! type Op = Op;
//! type Ret = Ret;
//!
//! fn new() -> Self {
//! Self { x: false, y: false }
//! }
//!
//! fn exec(&mut self, op: Op) -> Ret {
//! match op {
//! Op::WriteX => {
//! self.x = true;
//! Ret::Write
//! }
//! Op::WriteY => {
//! self.y = true;
//! Ret::Write
//! }
//! Op::ReadX => Ret::Read(self.x),
//! Op::ReadY => Ret::Read(self.y),
//! }
//! }
//! }
//!
//!
//! // Notice that the concurrent specification receives a shared reference to itself (`&self`)
//! // while the sequential specification receives an exclusive reference to itself (`&mut self`).
//! // This is because the concurrent specification is shared between threads
//! // while the sequential specification is not.
//!
//! // We are now ready to write our test:
//! #[test]
//! fn two_slots() {
//! Lincheck {
//! num_threads: 2,
//! num_ops: 5,
//! }.verify::<TwoSlotsParallel, TwoSlotsSequential>();
//! }
//! ```
//!
//! If we run the test, we get a failure along with a trace of the execution:
//! ```text
//! running 1 test
//! test two_slots ... FAILED
//!
//! failures:
//!
//! ---- two_slots stdout ----
//! thread 'two_slots' panicked at 'Non-linearizable execution:
//!
//! INIT PART:
//! |================|
//! | MAIN THREAD |
//! |================|
//! | |
//! | WriteX : Write |
//! | |
//! |----------------|
//!
//! PARALLEL PART:
//! |=====================|================|
//! | THREAD 0 | THREAD 1 |
//! |=====================|================|
//! | | |
//! | |----------------|
//! | | |
//! | ReadY : Read(false) | WriteY : Write |
//! | | |
//! | |----------------|
//! | | |
//! |---------------------| |
//! | | |
//! | ReadY : Read(false) | |
//! | | |
//! |---------------------|----------------|
//!
//! POST PART:
//! |================|
//! | MAIN THREAD |
//! |================|
//! | |
//! | WriteX : Write |
//! | |
//! |----------------|
//! ```
//!
//! # Limitations
//!
//! - Lincheck runner sets its own panic hook. This doesn't play well with parallel test execution. To fix this, you can run your tests with the `--test-threads=1` flag like this:
//! ```bash
//! $ cargo test -- --test-threads=1
//! ```
//! - [loom](https://github.com/tokio-rs/loom) can't model all weak memory models effects. This means that some executions that may arise on the real hardware may not be explored by loom. This is why the concurrent data structures should be additionally fuzzed on the real hardware. The support for fuzzing in Lincheck is planned.
//! - [proptest](https://docs.rs/proptest/latest/proptest/) only explores a random sample of all possible scenarios. This means that some failing executions may not be explored.
use ;
use UnwindSafe;
use ;
pub use *;
use *;
pub use *;
/// A test runner for Lincheck.
/// It is used to configure the test.