stateright 0.31.0

A model checker for implementing distributed systems.
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Private module for selective re-export.

use crate::checker::{Checker, Expectation, Path};
use crate::{fingerprint, CheckerBuilder, CheckerVisitor, Model, Property};
use dashmap::DashMap;
use rand::rngs::StdRng;
use rand::Rng;
use rand::SeedableRng;
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::{sleep, JoinHandle};
use std::time::{Duration, SystemTime};

use super::EventuallyBits;

/// Choose transitions in the model.
///
/// Created once for each thread.
pub trait Chooser<M: Model>: Send + Clone + 'static {
    /// State of the chooser during a run.
    type State;

    /// Create a new chooser state from this seed for the run.
    fn new_state(&self, seed: u64) -> Self::State;

    /// Choose the initial state for this simulation run.
    fn choose_initial_state(&self, state: &mut Self::State, initial_states: &[M::State]) -> usize;

    /// Choose the next action to take from the current state.
    fn choose_action(
        &self,
        state: &mut Self::State,
        current_state: &M::State,
        actions: &[M::Action],
    ) -> usize;
}

/// A chooser that makes uniform choices.
#[derive(Clone)]
pub struct UniformChooser;

/// A chooser that makes uniform choices.
pub struct UniformChooserState {
    // FIXME: use a reproducible rng, one that will not change over versions.
    rng: StdRng,
}

impl<M> Chooser<M> for UniformChooser
where
    M: Model,
{
    type State = UniformChooserState;

    fn new_state(&self, seed: u64) -> Self::State {
        UniformChooserState {
            rng: StdRng::seed_from_u64(seed),
        }
    }

    fn choose_initial_state(
        &self,
        state: &mut Self::State,
        initial_states: &[<M as Model>::State],
    ) -> usize {
        state.rng.random_range(0..initial_states.len())
    }

    fn choose_action(
        &self,
        state: &mut Self::State,
        _current_state: &<M as Model>::State,
        actions: &[<M as Model>::Action],
    ) -> usize {
        state.rng.random_range(0..actions.len())
    }
}

pub(crate) struct SimulationChecker<M: Model> {
    // Immutable state.
    model: Arc<M>,
    handles: Vec<std::thread::JoinHandle<()>>,

    // Mutable state.
    state_count: Arc<AtomicUsize>,
    max_depth: Arc<AtomicUsize>,
    discoveries: Arc<DashMap<&'static str, Vec<usize>>>,
}

impl<M> SimulationChecker<M>
where
    M: Model + Send + Sync + 'static,
    M::State: Hash + Send + 'static,
{
    /// Create a simulation checker.
    ///
    /// `seed` is the seed for the random selection of actions between states.
    /// It is passed straight through to the first trace on the first thread to allow for
    /// reproducibility. For other threads and traces it is regenerated using a [`StdRng`].
    pub(crate) fn spawn<C: Chooser<M>>(options: CheckerBuilder<M>, seed: u64, chooser: C) -> Self
    where
        M::State: Clone + PartialEq,
        M::Action: Clone + PartialEq,
    {
        let model = Arc::new(options.model);
        let symmetry = options.symmetry;
        let target_state_count = options.target_state_count;
        let target_max_depth = options.target_max_depth;
        let visitor = Arc::new(options.visitor);
        let finish_when = Arc::new(options.finish_when);
        let properties = Arc::new(model.properties());

        let state_count = Arc::new(AtomicUsize::new(0));
        let max_depth = Arc::new(AtomicUsize::new(0));
        let discoveries = Arc::new(DashMap::default());
        let mut handles = Vec::new();

        let mut thread_seed = seed;

        let shutdown = Arc::new(AtomicBool::new(false));
        let sd = Arc::clone(&shutdown);
        if let Some(close_after) = options.timeout {
            let closing_time = SystemTime::now() + close_after;
            std::thread::Builder::new()
                .name("timeout".to_owned())
                .spawn(move || loop {
                    let now = SystemTime::now();
                    if closing_time < now {
                        log::debug!("Reached timeout, triggering shutdown");
                        sd.store(true, Ordering::Relaxed);
                    }
                    if sd.load(Ordering::Relaxed) {
                        break;
                    }
                    sleep(Duration::from_secs(1));
                })
                .unwrap();
        }

        for t in 0..options.thread_count {
            let model = Arc::clone(&model);
            let visitor = Arc::clone(&visitor);
            let finish_when = Arc::clone(&finish_when);
            let properties = Arc::clone(&properties);
            let state_count = Arc::clone(&state_count);
            let max_depth = Arc::clone(&max_depth);
            let discoveries = Arc::clone(&discoveries);
            let shutdown = Arc::clone(&shutdown);
            let chooser = chooser.clone();
            handles.push(
                std::thread::Builder::new()
                    .name(format!("checker-{t}"))
                    .spawn(move || {
                        let mut seed = thread_seed;
                        log::debug!("{t}: Thread started with seed={seed}.");
                        // FIXME: use a reproducible rng, one that will not change over versions.
                        let mut rng = StdRng::seed_from_u64(seed);
                        loop {
                            if shutdown.load(Ordering::Relaxed) {
                                log::debug!("{t}: Got shutdown signal.");
                                break;
                            }

                            Self::check_trace_from_initial::<C>(
                                &model,
                                seed,
                                &chooser,
                                &state_count,
                                &discoveries,
                                &visitor,
                                target_max_depth,
                                &max_depth,
                                symmetry,
                            );

                            // Check whether we have found everything.
                            // All threads should reach this check and have the same result,
                            // leading them all to shut down together.
                            if finish_when.matches(
                                &discoveries.iter().map(|r| *r.key()).collect(),
                                &properties,
                            ) {
                                log::debug!("{t}: Discovery complete. Shutting down...");
                                return;
                            }
                            if let Some(target_state_count) = target_state_count {
                                if target_state_count.get() <= state_count.load(Ordering::Relaxed) {
                                    log::debug!(
                                        "{t}: Reached target state count. Shutting down..."
                                    );
                                    return;
                                }
                            }

                            seed = rng.random();
                            log::trace!("{t}: Generated new thread seed={seed}");
                        }
                    })
                    .expect("Failed to spawn a thread"),
            );
            thread_seed += 1;
        }
        SimulationChecker {
            model,
            handles,
            state_count,
            max_depth,
            discoveries,
        }
    }

    #[allow(clippy::too_many_arguments)]
    #[allow(clippy::type_complexity)]
    fn check_trace_from_initial<C: Chooser<M>>(
        model: &M,
        seed: u64,
        chooser: &C,
        state_count: &AtomicUsize,
        discoveries: &DashMap<&'static str, Vec<usize>>,
        visitor: &Option<Box<dyn CheckerVisitor<M> + Send + Sync>>,
        target_max_depth: Option<NonZeroUsize>,
        global_max_depth: &AtomicUsize,
        symmetry: Option<fn(&M::State) -> M::State>,
    ) where
        M::State: Clone + PartialEq,
        M::Action: Clone + PartialEq,
    {
        let properties = model.properties();

        let mut chooser_state = chooser.new_state(seed);

        let mut action_path = Vec::new();
        let mut state = {
            let mut initial_states = model.init_states();
            let index = chooser.choose_initial_state(&mut chooser_state, &initial_states);
            action_path.push(index);
            initial_states.swap_remove(index)
        };

        let mut current_max_depth = global_max_depth.load(Ordering::Relaxed);
        // The set of actions.
        let mut actions = Vec::new();
        // The fingerprints we've seen in this run, for preventing cycles.
        let mut generated = HashSet::new();
        let mut ebits = {
            let mut ebits = EventuallyBits::new();
            for (i, p) in model.properties().iter().enumerate() {
                if let Property {
                    expectation: Expectation::Eventually,
                    ..
                } = p
                {
                    ebits.insert(i);
                }
            }
            ebits
        };
        'outer: loop {
            if action_path.len() - 1 > current_max_depth {
                let _ = global_max_depth.compare_exchange(
                    current_max_depth,
                    action_path.len() - 1,
                    Ordering::Relaxed,
                    Ordering::Relaxed,
                );
                current_max_depth = action_path.len() - 1;
            }
            if let Some(target_max_depth) = target_max_depth {
                if action_path.len() > target_max_depth.get() {
                    log::trace!(
                        "Skipping exploring more states as past max depth {}",
                        action_path.len() - 1
                    );
                    // return not break here as we do not know if this is terminal.
                    log::trace!("Reached max depth");
                    return;
                }
            }

            // Skip if outside boundary.
            if !model.within_boundary(&state) {
                log::trace!("Found state outside of boundary");
                break;
            }

            // check that we haven't already seen this state
            let inserted = if let Some(representative) = symmetry {
                generated.insert(fingerprint(&representative(&state)))
            } else {
                generated.insert(fingerprint(&state))
            };
            if !inserted {
                // found a loop
                log::trace!("Found a loop");
                break;
            }

            state_count.fetch_add(1, Ordering::Relaxed);

            if let Some(visitor) = visitor {
                visitor.visit(
                    model,
                    Path::from_action_indices(model, VecDeque::from(action_path.clone())),
                );
            }

            // Done if discoveries found for all properties.
            let mut is_awaiting_discoveries = false;
            for (i, property) in properties.iter().enumerate() {
                if discoveries.contains_key(property.name)
                    && (property.expectation == Expectation::Eventually)
                    && !ebits.contains(i)
                {
                    continue;
                }
                match property {
                    Property {
                        expectation: Expectation::Always,
                        condition: always,
                        ..
                    } => {
                        if !always(model, &state) {
                            // Races other threads, but that's fine.
                            discoveries.insert(property.name, action_path.clone());
                        } else {
                            is_awaiting_discoveries = true;
                        }
                    }
                    Property {
                        expectation: Expectation::Sometimes,
                        condition: sometimes,
                        ..
                    } => {
                        if sometimes(model, &state) {
                            // Races other threads, but that's fine.
                            discoveries.insert(property.name, action_path.clone());
                        } else {
                            is_awaiting_discoveries = true;
                        }
                    }
                    Property {
                        expectation: Expectation::Eventually,
                        condition: eventually,
                        ..
                    } => {
                        // The checker early exits after finding discoveries for every property,
                        // and "eventually" property discoveries are only identified at terminal
                        // states, so if we are here it means we are still awaiting a corresponding
                        // discovery regardless of whether the eventually property is now satisfied
                        // (i.e. it might be falsifiable via a different path).
                        is_awaiting_discoveries = true;
                        if eventually(model, &state) {
                            ebits.remove(i);
                        }
                    }
                }
            }
            if !is_awaiting_discoveries {
                log::trace!("Found all discoveries");
                break;
            }

            // generate the possible next actions
            model.actions(&state, &mut actions);

            // generate the next state, repeatedly choosing an action until we get one or there are
            // no actions left to choose.
            loop {
                if actions.is_empty() {
                    // no actions to choose from
                    // break from the outer loop so that we still check eventually properties
                    log::trace!("No actions to choose from");
                    break 'outer;
                }

                // now pick one
                let index = chooser.choose_action(&mut chooser_state, &state, &actions);
                let action = actions.swap_remove(index);

                // take the chosen action
                match model.next_state(&state, action) {
                    None => {
                        // this action was ignored, try and choose another
                        action_path.push(index);
                        log::trace!("No next state");
                    }
                    Some(next_state) => {
                        action_path.push(index);
                        // now clear the actions for the next round
                        actions.clear();
                        state = next_state;
                        break;
                    }
                };
            }
        }
        // check the eventually properties
        for (i, property) in properties.iter().enumerate() {
            if ebits.contains(i) {
                // Races other threads, but that's fine.
                discoveries.insert(property.name, action_path.clone());
            }
        }
    }
}

impl<M> Checker<M> for SimulationChecker<M>
where
    M: Model,
    M::State: Hash,
{
    fn model(&self) -> &M {
        &self.model
    }

    fn state_count(&self) -> usize {
        self.state_count.load(Ordering::Relaxed)
    }

    fn unique_state_count(&self) -> usize {
        // we do not keep track of all the states visited so can't provide an accurate unique state
        // count
        self.state_count.load(Ordering::Relaxed)
    }

    fn max_depth(&self) -> usize {
        self.max_depth.load(Ordering::Relaxed)
    }

    fn discoveries(&self) -> HashMap<&'static str, Path<M::State, M::Action>>
    where
        M::State: Clone + PartialEq,
        M::Action: Clone + PartialEq,
    {
        self.discoveries
            .iter()
            .map(|mapref| {
                (
                    <&'static str>::clone(mapref.key()),
                    Path::from_action_indices(self.model(), VecDeque::from(mapref.value().clone())),
                )
            })
            .collect()
    }

    fn handles(&mut self) -> Vec<JoinHandle<()>> {
        std::mem::take(&mut self.handles)
    }

    fn is_done(&self) -> bool {
        self.handles.iter().all(|h| h.is_finished())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_util::linear_equation_solver::*;

    #[test]
    fn can_complete_by_eliminating_properties() {
        let checker = LinearEquation { a: 2, b: 10, c: 14 }
            .checker()
            .spawn_simulation(0, UniformChooser)
            .join();
        checker.assert_properties();

        checker.assert_discovery(
            "solvable",
            vec![Guess::IncreaseX, Guess::IncreaseY, Guess::IncreaseX],
        );
    }
}