riri 0.8.0

A Miri-style undefined-behaviour detector for Rust GPU kernels: run SIMT kernels on the CPU and catch data races, barrier and warp divergence, bad shuffle masks, and uninitialised reads of shared or global memory, understands release and acquire ordering between blocks, and shrinks a failing schedule to the few decisions that cause it.
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
use std::any::Any;
use std::ops::{Add, Deref, DerefMut, Sub};
use std::panic::Location;
use std::sync::{Arc, Mutex, MutexGuard};

use crate::ctx::ThreadCtx;
use crate::diag::{AccessKind, Diagnostic, MemSpace};
use crate::shadow::Cell;
use crate::sync::Ordering;

/// Shared implementation for any instrumented array.
struct Instrumented<T> {
    data: Vec<T>,
    shadow: Vec<Cell>,
    /// Launch the shadow state belongs to; reset when a new launch touches it.
    launch_id: u64,
}

/// What the access path needs to know about a buffer: where its data lives,
/// how to name it in a diagnostic, and how to clear its shadow state when a
/// new launch first touches it.
///
/// These three travel together at every call site, so they are one argument.
struct Buffer<'a, T, S, R> {
    mem: &'a Mutex<Instrumented<T>>,
    space: S,
    reset: R,
}

fn checked_access<T, S, R>(
    buf: Buffer<'_, T, S, R>,
    ctx: &ThreadCtx<'_>,
    index: usize,
    kind: AccessKind,
    location: &'static Location<'static>,
    ordering: Option<Ordering>,
    op: impl FnOnce(&mut T) -> T,
) -> T
where
    T: Copy,
    S: Fn() -> MemSpace,
    R: Fn(&mut Instrumented<T>),
{
    let Buffer { mem, space, reset } = buf;

    // Let another thread run first: this is where interleavings come from.
    ctx.schedule_point();

    let mut m = mem.lock().unwrap();
    if m.launch_id != ctx.launch_id() {
        reset(&mut m);
        m.launch_id = ctx.launch_id();
    }
    let len = m.data.len();
    if index >= len {
        drop(m);
        let (access, _) = ctx.access(kind, location);
        ctx.trap(Diagnostic::OutOfBounds {
            space: space(),
            index,
            len,
            access,
        });
    }

    // An acquire has to land before this access is weighed against earlier
    // ones, or it would not order the very access that performed it.
    if let Some(ordering) = ordering {
        let published = m.shadow[index].release.clone();
        ctx.acquire_from(&published, ordering);
    }

    let (acc, now) = ctx.access(kind, location);
    let cell = &mut m.shadow[index];
    let was_init = cell.init;
    let conflict = match kind {
        AccessKind::Read => cell.on_read(acc, &now),
        AccessKind::Write | AccessKind::Atomic => cell.on_write(acc, &now),
    };
    let value = op(&mut m.data[index]);
    if let Some(ordering) = ordering {
        ctx.release_into(&mut m.shadow[index].release, ordering);
    }
    drop(m);

    if kind != AccessKind::Write && !was_init {
        ctx.report(Diagnostic::UninitRead {
            space: space(),
            index,
            access: acc,
        });
    }
    if let Some(first) = conflict {
        ctx.report(Diagnostic::DataRace {
            space: space(),
            index,
            first,
            second: acc,
        });
    }
    value
}

// ---------------------------------------------------------------- global ---

/// A buffer in simulated global (device) memory.
///
/// Cheap to clone (reference-counted), so kernels can capture it by value or
/// by reference. Contents persist across launches; shadow state does not.
pub struct GlobalBuf<T> {
    name: Arc<str>,
    mem: Arc<Mutex<Instrumented<T>>>,
}

impl<T> Clone for GlobalBuf<T> {
    fn clone(&self) -> Self {
        GlobalBuf {
            name: self.name.clone(),
            mem: self.mem.clone(),
        }
    }
}

impl<T: Copy + Send + 'static> GlobalBuf<T> {
    /// Creates a buffer initialised from host data (like a host-to-device copy).
    pub fn new(name: &str, data: Vec<T>) -> Self {
        let shadow = vec![Cell::initialised(); data.len()];
        GlobalBuf {
            name: name.into(),
            mem: Arc::new(Mutex::new(Instrumented {
                data,
                shadow,
                launch_id: 0,
            })),
        }
    }

    /// Creates a buffer of device memory that nothing has written yet, the way
    /// an output allocation arrives before a kernel fills it.
    ///
    /// Reading an element before some thread has written it is reported as
    /// [`Diagnostic::UninitRead`], which is the check Compute Sanitizer's
    /// `initcheck` performs. The usual bug it finds is a kernel that fills
    /// only part of its output, leaving the rest to be read as whatever the
    /// allocator last left there.
    ///
    /// The elements hold `T::default()` so that something is there to read;
    /// those values carry no meaning, and a read that reaches them is the
    /// fault being reported. Once written, an element stays initialised for
    /// the rest of the buffer's life, including across later launches, since
    /// that is how device memory behaves.
    ///
    /// [`Diagnostic::UninitRead`]: crate::Diagnostic::UninitRead
    pub fn uninit(name: &str, len: usize) -> Self
    where
        T: Default,
    {
        GlobalBuf {
            name: name.into(),
            mem: Arc::new(Mutex::new(Instrumented {
                data: (0..len).map(|_| T::default()).collect(),
                shadow: vec![Cell::default(); len],
                launch_id: 0,
            })),
        }
    }

    pub fn len(&self) -> usize {
        self.mem.lock().unwrap().data.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Copies the contents back to the host.
    pub fn to_vec(&self) -> Vec<T> {
        self.mem.lock().unwrap().data.clone()
    }

    fn space(&self) -> MemSpace {
        MemSpace::Global {
            buffer: self.name.clone(),
        }
    }

    fn reset(m: &mut Instrumented<T>) {
        // Global memory keeps its (initialised) values across launches;
        // only the access history is launch-local.
        for c in &mut m.shadow {
            let init = c.init;
            *c = Cell::default();
            c.init = init;
        }
    }

    #[track_caller]
    pub fn read(&self, ctx: &ThreadCtx<'_>, index: usize) -> T {
        checked_access(
            Buffer {
                mem: &self.mem,
                space: || self.space(),
                reset: Self::reset,
            },
            ctx,
            index,
            AccessKind::Read,
            Location::caller(),
            None,
            |v| *v,
        )
    }

    #[track_caller]
    pub fn write(&self, ctx: &ThreadCtx<'_>, index: usize, value: T) {
        checked_access(
            Buffer {
                mem: &self.mem,
                space: || self.space(),
                reset: Self::reset,
            },
            ctx,
            index,
            AccessKind::Write,
            Location::caller(),
            None,
            |v| {
                *v = value;
                value
            },
        );
    }
}

impl<T: Copy + Send + 'static> GlobalBuf<T> {
    /// An atomic read. With [`Ordering::Acquire`] it takes on whatever a
    /// matching release published.
    #[track_caller]
    pub fn atomic_load(&self, ctx: &ThreadCtx<'_>, index: usize, ordering: Ordering) -> T {
        self.atomic_load_at(ctx, index, ordering, Location::caller())
    }

    pub(crate) fn atomic_load_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) -> T {
        self.atomic_rmw(ctx, index, ordering, at, |v| *v)
    }

    /// An atomic write. With [`Ordering::Release`], or after a release fence,
    /// it publishes everything this thread did beforehand.
    #[track_caller]
    pub fn atomic_store(&self, ctx: &ThreadCtx<'_>, index: usize, value: T, ordering: Ordering) {
        self.atomic_store_at(ctx, index, value, ordering, Location::caller());
    }

    pub(crate) fn atomic_store_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        value: T,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) {
        self.atomic_rmw(ctx, index, ordering, at, |v| {
            *v = value;
            value
        });
    }
}

impl<T: Copy + Send + 'static> GlobalBuf<T> {
    /// The read-modify-write every atomic below is a shape of.
    ///
    /// The update runs while the element is held, so nothing can interleave
    /// between reading the old value and writing the new one. That is what
    /// makes it atomic here, and it is why a compare-and-exchange can decide
    /// and act in one step.
    /// `at` is passed in rather than captured, because the shim reaches these
    /// through a closure and `#[track_caller]` does not survive one.
    pub(crate) fn atomic_rmw(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        ordering: Ordering,
        at: &'static Location<'static>,
        update: impl FnOnce(&mut T) -> T,
    ) -> T {
        checked_access(
            Buffer {
                mem: &self.mem,
                space: || self.space(),
                reset: Self::reset,
            },
            ctx,
            index,
            AccessKind::Atomic,
            at,
            Some(ordering),
            update,
        )
    }

    /// `atomicExch`: writes `value` and returns what was there.
    #[track_caller]
    pub fn atomic_swap(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        value: T,
        ordering: Ordering,
    ) -> T {
        self.atomic_swap_at(ctx, index, value, ordering, Location::caller())
    }

    pub(crate) fn atomic_swap_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        value: T,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) -> T {
        self.atomic_rmw(ctx, index, ordering, at, |v| std::mem::replace(v, value))
    }
}

impl<T: Copy + Send + PartialEq + 'static> GlobalBuf<T> {
    /// `atomicCAS`: writes `new` only if the element still holds `current`.
    ///
    /// Returns the previous value either way, as `Ok` when the exchange
    /// happened and `Err` when it did not, matching `std`.
    #[track_caller]
    pub fn atomic_compare_exchange(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        current: T,
        new: T,
        ordering: Ordering,
    ) -> Result<T, T> {
        self.atomic_compare_exchange_at(ctx, index, current, new, ordering, Location::caller())
    }

    pub(crate) fn atomic_compare_exchange_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        current: T,
        new: T,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) -> Result<T, T> {
        let mut exchanged = false;
        let previous = self.atomic_rmw(ctx, index, ordering, at, |v| {
            let previous = *v;
            if previous == current {
                *v = new;
                exchanged = true;
            }
            previous
        });
        if exchanged {
            Ok(previous)
        } else {
            Err(previous)
        }
    }
}

impl<T: Copy + Send + Ord + 'static> GlobalBuf<T> {
    /// `atomicMin`, returning the previous value.
    #[track_caller]
    pub fn atomic_min(&self, ctx: &ThreadCtx<'_>, index: usize, value: T, ordering: Ordering) -> T {
        self.atomic_min_at(ctx, index, value, ordering, Location::caller())
    }

    pub(crate) fn atomic_min_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        value: T,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) -> T {
        self.atomic_rmw(ctx, index, ordering, at, |v| {
            let previous = *v;
            if value < previous {
                *v = value;
            }
            previous
        })
    }

    /// `atomicMax`, returning the previous value.
    #[track_caller]
    pub fn atomic_max(&self, ctx: &ThreadCtx<'_>, index: usize, value: T, ordering: Ordering) -> T {
        self.atomic_max_at(ctx, index, value, ordering, Location::caller())
    }

    pub(crate) fn atomic_max_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        value: T,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) -> T {
        self.atomic_rmw(ctx, index, ordering, at, |v| {
            let previous = *v;
            if value > previous {
                *v = value;
            }
            previous
        })
    }
}

impl<T: Copy + Send + Sub<Output = T> + 'static> GlobalBuf<T> {
    /// `atomicSub`, returning the previous value.
    #[track_caller]
    pub fn atomic_sub(&self, ctx: &ThreadCtx<'_>, index: usize, value: T, ordering: Ordering) -> T {
        self.atomic_sub_at(ctx, index, value, ordering, Location::caller())
    }

    pub(crate) fn atomic_sub_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        value: T,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) -> T {
        self.atomic_rmw(ctx, index, ordering, at, |v| {
            let previous = *v;
            *v = previous - value;
            previous
        })
    }
}

impl<T: Copy + Send + Add<Output = T> + 'static> GlobalBuf<T> {
    /// `atomicAdd`: returns the previous value. Atomics never race with each
    /// other, only with concurrent plain accesses.
    #[track_caller]
    pub fn atomic_add(&self, ctx: &ThreadCtx<'_>, index: usize, value: T, ordering: Ordering) -> T {
        self.atomic_add_at(ctx, index, value, ordering, Location::caller())
    }

    pub(crate) fn atomic_add_at(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        value: T,
        ordering: Ordering,
        at: &'static Location<'static>,
    ) -> T {
        self.atomic_rmw(ctx, index, ordering, at, |v| {
            let previous = *v;
            *v = previous + value;
            previous
        })
    }
}

/// A borrowed element of an instrumented buffer, held for as long as the
/// caller keeps it.
///
/// This exists so a surface that hands out `&mut T`, as cuda-oxide's
/// `DisjointSlice::get_mut` does, can be offered without Riri giving up
/// either its instrumentation or its freedom from `unsafe`. The lock is held
/// for the life of the borrow, which is sound here because only the thread
/// holding the turn runs. Holding one across a barrier would wedge the
/// launch, so do not.
pub struct ElemMut<'a, T> {
    guard: MutexGuard<'a, Instrumented<T>>,
    index: usize,
}

impl<T> Deref for ElemMut<'_, T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.guard.data[self.index]
    }
}

impl<T> DerefMut for ElemMut<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.guard.data[self.index]
    }
}

impl<T: Copy + Send + 'static> GlobalBuf<T> {
    /// Borrows one element for writing, recording the access first.
    ///
    /// `checked` decides what an out-of-range index means: `None` for a
    /// bounds-checked surface, or a trap for an unchecked one.
    ///
    /// `location` is passed in rather than captured, because the caller is
    /// usually reached through a closure and `#[track_caller]` does not
    /// survive one. Reporting a race inside Riri instead of in the kernel
    /// would defeat the point of reporting it at all.
    pub(crate) fn elem_mut(
        &self,
        ctx: &ThreadCtx<'_>,
        index: usize,
        checked: bool,
        location: &'static Location<'static>,
    ) -> Option<ElemMut<'_, T>> {
        ctx.schedule_point();

        let (acc, now) = ctx.access(AccessKind::Write, location);
        let mut m = self.mem.lock().unwrap();
        if m.launch_id != ctx.launch_id() {
            Self::reset(&mut m);
            m.launch_id = ctx.launch_id();
        }
        let len = m.data.len();
        if index >= len {
            drop(m);
            if checked {
                return None;
            }
            ctx.trap(Diagnostic::OutOfBounds {
                space: self.space(),
                index,
                len,
                access: acc,
            });
        }

        // Reporting takes a different lock, so this is safe to do while the
        // element stays borrowed.
        if let Some(first) = m.shadow[index].on_write(acc, &now) {
            ctx.report(Diagnostic::DataRace {
                space: self.space(),
                index,
                first,
                second: acc,
            });
        }
        Some(ElemMut { guard: m, index })
    }
}

// ---------------------------------------------------------------- shared ---

pub(crate) struct SharedInner<T> {
    block: u32,
    name: &'static str,
    mem: Mutex<Instrumented<T>>,
}

/// A block-wide `__shared__` array. Obtain one with [`ThreadCtx::shared`].
pub struct SharedArray<T> {
    inner: Arc<SharedInner<T>>,
}

impl<T> Clone for SharedArray<T> {
    fn clone(&self) -> Self {
        SharedArray {
            inner: self.inner.clone(),
        }
    }
}

impl<T: Copy + Default + Send + 'static> SharedArray<T> {
    pub(crate) fn new_inner(block: u32, name: &'static str, len: usize) -> SharedInner<T> {
        SharedInner {
            block,
            name,
            mem: Mutex::new(Instrumented {
                data: vec![T::default(); len],
                shadow: vec![Cell::default(); len],
                launch_id: 0,
            }),
        }
    }

    pub(crate) fn from_any(a: Arc<dyn Any + Send + Sync>) -> Option<Self> {
        a.downcast::<SharedInner<T>>()
            .ok()
            .map(|inner| SharedArray { inner })
    }

    pub fn len(&self) -> usize {
        self.inner.mem.lock().unwrap().data.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn space(&self) -> MemSpace {
        MemSpace::Shared {
            block: self.inner.block,
            name: self.inner.name,
        }
    }

    fn reset(m: &mut Instrumented<T>) {
        // Shared memory lives for one launch: it starts uninitialised.
        for c in &mut m.shadow {
            *c = Cell::default();
        }
    }

    #[track_caller]
    pub fn read(&self, ctx: &ThreadCtx<'_>, index: usize) -> T {
        checked_access(
            Buffer {
                mem: &self.inner.mem,
                space: || self.space(),
                reset: Self::reset,
            },
            ctx,
            index,
            AccessKind::Read,
            Location::caller(),
            None,
            |v| *v,
        )
    }

    #[track_caller]
    pub fn write(&self, ctx: &ThreadCtx<'_>, index: usize, value: T) {
        checked_access(
            Buffer {
                mem: &self.inner.mem,
                space: || self.space(),
                reset: Self::reset,
            },
            ctx,
            index,
            AccessKind::Write,
            Location::caller(),
            None,
            |v| {
                *v = value;
                value
            },
        );
    }
}