corescout-mirror 1.1.0

M(t): the computational mirror and its shared-memory self-state plane.
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
//! The shared memory region behind the self-state plane.
//!
//! # Read-only is enforced by the kernel, not by convention
//!
//! This is the architectural point of the whole module. The writer maps the
//! region `PROT_READ | PROT_WRITE`; every consumer opens the file `O_RDONLY` and
//! maps it `PROT_READ`. A consumer that tries to modify the mirror takes a
//! `SIGSEGV` from the MMU.
//!
//! That matters because the alternative, a `&` rather than a `&mut` in an API,
//! only protects consumers written in Rust that go through the API. The mirror
//! is meant to be read by arbitrary software, including software not written
//! yet and not written in this language. The truth boundary between "reality"
//! and "an agent's representation of reality" should not depend on the agent's
//! good manners, so it is placed where the hardware enforces it.
//!
//! # Where the region lives
//!
//! `$XDG_RUNTIME_DIR/corescout/mirror.plane` when that exists, otherwise
//! `/dev/shm/corescout-mirror.plane`. Both are tmpfs, so the plane is shared
//! memory that happens to have a path: no daemon registry, no socket, no
//! discovery protocol. A consumer that knows the path can map it, and the
//! filesystem's own permissions decide who may.

use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};

use crate::plane::layout::OFF_SEQ;
use corescout_core::error::{Error, Result};

/// Default path of the shared plane.
pub fn default_path() -> PathBuf {
    if let Some(runtime) = std::env::var_os("XDG_RUNTIME_DIR") {
        if !runtime.is_empty() {
            return PathBuf::from(runtime)
                .join("corescout")
                .join("mirror.plane");
        }
    }
    PathBuf::from("/dev/shm/corescout-mirror.plane")
}

/// How a [`PlaneMemory`] is backed.
enum Backing {
    /// A private allocation. Used by tests and by a mirror running without
    /// publishing. `Vec<u64>` rather than `Vec<u8>` so the region is 8-byte
    /// aligned and the seqlock's atomic access is well defined.
    ///
    /// The vector is never read through this field: it exists to own the
    /// allocation that `ptr` points into.
    Owned(#[allow(dead_code)] Vec<u64>),
    #[cfg(target_os = "linux")]
    Mapped {
        addr: *mut libc::c_void,
        len: usize,
        /// Held open so the mapping keeps the inode alive.
        _file: std::fs::File,
    },
    /// A Windows file mapping. Two handles rather than one: the section object
    /// and the view into it, and both have to be released.
    #[cfg(target_os = "windows")]
    Section {
        view: *mut core::ffi::c_void,
        mapping: windows_sys::Win32::Foundation::HANDLE,
        file: windows_sys::Win32::Foundation::HANDLE,
    },
}

/// A mapped or allocated plane region.
pub struct PlaneMemory {
    ptr: *mut u8,
    len: usize,
    writable: bool,
    backing: Backing,
}

// SAFETY: `PlaneMemory` owns its region exclusively. Cross-process sharing is
// handled by the seqlock protocol in the writer and reader, not by Rust's
// aliasing rules, which cannot describe another process's memory accesses
// anyway.
unsafe impl Send for PlaneMemory {}

impl std::fmt::Debug for PlaneMemory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PlaneMemory")
            .field("len", &self.len)
            .field("writable", &self.writable)
            .field(
                "backing",
                &match &self.backing {
                    Backing::Owned(_) => "owned",
                    #[cfg(target_os = "linux")]
                    Backing::Mapped { .. } => "mapped",
                    #[cfg(target_os = "windows")]
                    Backing::Section { .. } => "section",
                },
            )
            .finish()
    }
}

impl PlaneMemory {
    /// A private, writable region. Portable, and not shared with anyone.
    pub fn anonymous(len: usize) -> PlaneMemory {
        let words = len.div_ceil(8).max(1);
        let mut backing = vec![0u64; words];
        let ptr = backing.as_mut_ptr() as *mut u8;
        PlaneMemory {
            ptr,
            len,
            writable: true,
            backing: Backing::Owned(backing),
        }
    }

    /// Create or resize the shared region and map it writable.
    #[cfg(target_os = "linux")]
    pub fn create(path: &std::path::Path, len: usize) -> Result<PlaneMemory> {
        use std::os::unix::io::AsRawFd;

        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| Error::io(parent, e))?;
        }
        let file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(path)
            .map_err(|e| Error::io(path, e))?;
        file.set_len(len as u64).map_err(|e| Error::io(path, e))?;

        // SAFETY: mapping `len` bytes of a file we just sized to `len`.
        let addr = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                len,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_SHARED,
                file.as_raw_fd(),
                0,
            )
        };
        if addr == libc::MAP_FAILED {
            return Err(Error::Syscall {
                call: "mmap",
                errno: std::io::Error::last_os_error().raw_os_error().unwrap_or(0),
            });
        }
        Ok(PlaneMemory {
            ptr: addr as *mut u8,
            len,
            writable: true,
            backing: Backing::Mapped {
                addr,
                len,
                _file: file,
            },
        })
    }

    /// Map an existing region read-only.
    ///
    /// The `PROT_READ` here is the truth boundary. Do not be tempted to make it
    /// writable for convenience.
    #[cfg(target_os = "linux")]
    pub fn open_read_only(path: &std::path::Path) -> Result<PlaneMemory> {
        use std::os::unix::io::AsRawFd;

        let file = std::fs::File::open(path).map_err(|e| Error::io(path, e))?;
        let len = file.metadata().map_err(|e| Error::io(path, e))?.len() as usize;
        if len < crate::plane::layout::HEADER_BYTES {
            return Err(Error::invalid(format!(
                "{} is too small to be a self-state plane",
                path.display()
            )));
        }

        // SAFETY: mapping `len` bytes of a file of exactly that size.
        let addr = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                len,
                libc::PROT_READ,
                libc::MAP_SHARED,
                file.as_raw_fd(),
                0,
            )
        };
        if addr == libc::MAP_FAILED {
            return Err(Error::Syscall {
                call: "mmap",
                errno: std::io::Error::last_os_error().raw_os_error().unwrap_or(0),
            });
        }
        Ok(PlaneMemory {
            ptr: addr as *mut u8,
            len,
            writable: false,
            backing: Backing::Mapped {
                addr,
                len,
                _file: file,
            },
        })
    }

    /// Create or reopen the shared region and map it writable, on Windows.
    ///
    /// # Why a file rather than a named section
    ///
    /// A page-file-backed section named in the kernel namespace would be
    /// simpler and would vanish when the last handle closed. The plane is
    /// deliberately a file on disk for the same reason it is on Linux: a
    /// consumer can find it by path, an operator can see it exists, and a
    /// recording of it is an ordinary file. A mirror nobody can locate is an
    /// internal data structure.
    #[cfg(target_os = "windows")]
    pub fn create(path: &std::path::Path, len: usize) -> Result<PlaneMemory> {
        use std::os::windows::io::AsRawHandle;
        use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};
        use windows_sys::Win32::System::Memory::{
            CreateFileMappingW, MapViewOfFile, FILE_MAP_READ, FILE_MAP_WRITE, PAGE_READWRITE,
        };

        if len == 0 {
            return Err(Error::invalid("a self-state plane cannot be zero bytes"));
        }
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).map_err(|source| Error::io(parent, source))?;
        }
        let file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(path)
            .map_err(|source| Error::io(path, source))?;
        // Size it before mapping: a view cannot be larger than the file.
        file.set_len(len as u64)
            .map_err(|source| Error::io(path, source))?;

        let handle = file.as_raw_handle() as windows_sys::Win32::Foundation::HANDLE;
        if handle == INVALID_HANDLE_VALUE {
            return Err(Error::invalid("the plane file has no usable handle"));
        }
        // SAFETY: a live file handle, a size that matches the file, and a null
        // name so the section is unnamed and reached only through this file.
        let mapping = unsafe {
            CreateFileMappingW(
                handle,
                std::ptr::null(),
                PAGE_READWRITE,
                (len >> 32) as u32,
                len as u32,
                std::ptr::null(),
            )
        };
        if mapping == 0 {
            return Err(Error::Syscall {
                call: "CreateFileMappingW",
                errno: std::io::Error::last_os_error().raw_os_error().unwrap_or(0),
            });
        }
        // SAFETY: a live section handle; the view is unmapped in `Drop`.
        let view = unsafe { MapViewOfFile(mapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, len) };
        if view.Value.is_null() {
            let error = std::io::Error::last_os_error();
            // SAFETY: closing the handle we just opened, before returning.
            unsafe {
                CloseHandle(mapping);
            }
            return Err(Error::Syscall {
                call: "MapViewOfFile",
                errno: error.raw_os_error().unwrap_or(0),
            });
        }

        // Keep the file handle alive for the life of the mapping by taking it
        // out of the `File`, which would otherwise close it on drop.
        let owned_file = handle;
        std::mem::forget(file);

        Ok(PlaneMemory {
            ptr: view.Value as *mut u8,
            len,
            writable: true,
            backing: Backing::Section {
                view: view.Value,
                mapping,
                file: owned_file,
            },
        })
    }

    /// Map an existing plane read-only.
    ///
    /// `FILE_MAP_READ` alone, so the pages are read-only to the MMU. A consumer
    /// that tried to write its own reflection would take an access violation,
    /// which is the same guarantee `PROT_READ` gives on Linux and the reason
    /// the mirror can be called read-only without qualification.
    #[cfg(target_os = "windows")]
    pub fn open_read_only(path: &std::path::Path) -> Result<PlaneMemory> {
        use std::os::windows::io::AsRawHandle;
        use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};
        use windows_sys::Win32::System::Memory::{
            CreateFileMappingW, MapViewOfFile, FILE_MAP_READ, PAGE_READONLY,
        };

        let file = std::fs::OpenOptions::new()
            .read(true)
            .open(path)
            .map_err(|source| Error::io(path, source))?;
        let len = file
            .metadata()
            .map_err(|source| Error::io(path, source))?
            .len() as usize;
        if len == 0 {
            return Err(Error::invalid("the self-state plane is empty"));
        }

        let handle = file.as_raw_handle() as windows_sys::Win32::Foundation::HANDLE;
        if handle == INVALID_HANDLE_VALUE {
            return Err(Error::invalid("the plane file has no usable handle"));
        }
        // SAFETY: a live read handle and a size taken from the file itself.
        let mapping = unsafe {
            CreateFileMappingW(
                handle,
                std::ptr::null(),
                PAGE_READONLY,
                0,
                0,
                std::ptr::null(),
            )
        };
        if mapping == 0 {
            return Err(Error::Syscall {
                call: "CreateFileMappingW (read-only)",
                errno: std::io::Error::last_os_error().raw_os_error().unwrap_or(0),
            });
        }
        // SAFETY: a live section handle, mapped without write access.
        let view = unsafe { MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0) };
        if view.Value.is_null() {
            let error = std::io::Error::last_os_error();
            // SAFETY: closing the handle we just opened.
            unsafe {
                CloseHandle(mapping);
            }
            return Err(Error::Syscall {
                call: "MapViewOfFile (read-only)",
                errno: error.raw_os_error().unwrap_or(0),
            });
        }

        let owned_file = handle;
        std::mem::forget(file);

        Ok(PlaneMemory {
            ptr: view.Value as *mut u8,
            len,
            writable: false,
            backing: Backing::Section {
                view: view.Value,
                mapping,
                file: owned_file,
            },
        })
    }

    #[cfg(not(any(target_os = "linux", target_os = "windows")))]
    pub fn create(_path: &std::path::Path, _len: usize) -> Result<PlaneMemory> {
        Err(Error::unsupported(
            "shared self-state plane (memory mapping is implemented for Linux and Windows)",
        ))
    }

    #[cfg(not(any(target_os = "linux", target_os = "windows")))]
    pub fn open_read_only(_path: &std::path::Path) -> Result<PlaneMemory> {
        Err(Error::unsupported(
            "shared self-state plane (memory mapping is implemented for Linux and Windows)",
        ))
    }

    pub fn len(&self) -> usize {
        self.len
    }

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

    pub fn is_writable(&self) -> bool {
        self.writable
    }

    /// The region as bytes.
    ///
    /// # A note on the memory model
    ///
    /// Another process may be writing these bytes concurrently. Strictly, that
    /// is a data race that Rust's model has no way to describe, because the
    /// other party is outside the model entirely. The seqlock is what makes it
    /// safe in practice: any read that overlapped a write is detected by the
    /// sequence check and discarded, so torn data is never acted on. This is the
    /// standard seqlock formulation, and the reason readers copy out of the
    /// region before interpreting anything.
    pub fn as_slice(&self) -> &[u8] {
        // SAFETY: `ptr` is valid for `len` bytes for the lifetime of `self`.
        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
    }

    /// The region as mutable bytes.
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        debug_assert!(self.writable, "attempted to write a read-only plane");
        // SAFETY: as above, and `self` is borrowed mutably.
        unsafe { std::slice::from_raw_parts_mut(self.ptr, self.len) }
    }

    /// The seqlock counter, as an atomic.
    ///
    /// Odd means a write is in progress. This is the only field either side
    /// touches atomically; everything else is ordered by the acquire/release
    /// pair around it.
    pub fn seq(&self) -> &AtomicU64 {
        debug_assert!(self.len >= OFF_SEQ + 8);
        // SAFETY: the header is at least `OFF_SEQ + 8` bytes, the region is
        // 8-byte aligned (page-aligned when mapped, `Vec<u64>`-aligned when
        // owned), and `AtomicU64` has the same layout as `u64`.
        unsafe { &*(self.ptr.add(OFF_SEQ) as *const AtomicU64) }
    }

    /// Read the seqlock counter with acquire ordering.
    pub fn load_seq(&self) -> u64 {
        self.seq().load(Ordering::Acquire)
    }
}

impl Drop for PlaneMemory {
    fn drop(&mut self) {
        match &self.backing {
            Backing::Owned(_) => {}
            #[cfg(target_os = "linux")]
            Backing::Mapped { addr, len, .. } => {
                // SAFETY: unmapping exactly what we mapped.
                unsafe {
                    libc::munmap(*addr, *len);
                }
            }
            #[cfg(target_os = "windows")]
            Backing::Section {
                view,
                mapping,
                file,
            } => {
                use windows_sys::Win32::Foundation::CloseHandle;
                use windows_sys::Win32::System::Memory::{
                    UnmapViewOfFile, MEMORY_MAPPED_VIEW_ADDRESS,
                };
                // SAFETY: releasing exactly the view and handles this value
                // owns, once, in the order Windows requires: view, then
                // section, then file.
                unsafe {
                    UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: *view });
                    CloseHandle(*mapping);
                    CloseHandle(*file);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn an_anonymous_region_is_zeroed_writable_and_aligned() {
        let mut memory = PlaneMemory::anonymous(1024);
        assert_eq!(memory.len(), 1024);
        assert!(memory.is_writable());
        assert!(memory.as_slice().iter().all(|b| *b == 0));
        assert_eq!(
            memory.as_mut_slice().as_ptr() as usize % 8,
            0,
            "the region must be 8-byte aligned for the seqlock atomic"
        );
    }

    #[test]
    fn writes_are_visible_through_the_read_view() {
        let mut memory = PlaneMemory::anonymous(256);
        memory.as_mut_slice()[7] = 0xAB;
        assert_eq!(memory.as_slice()[7], 0xAB);
    }

    #[test]
    fn the_seqlock_counter_is_addressable() {
        let memory = PlaneMemory::anonymous(256);
        assert_eq!(memory.load_seq(), 0);
        memory.seq().store(3, Ordering::Release);
        assert_eq!(memory.load_seq(), 3);
        // ...and it lives where the layout says it does.
        assert_eq!(crate::plane::layout::get_u64(memory.as_slice(), OFF_SEQ), 3);
    }

    #[test]
    fn default_path_follows_xdg_when_set() {
        std::env::set_var("XDG_RUNTIME_DIR", "/run/user/1000");
        let path = default_path();
        std::env::remove_var("XDG_RUNTIME_DIR");
        assert!(path.ends_with("mirror.plane"));
        assert!(path.to_string_lossy().contains("1000"));
    }

    #[test]
    #[cfg(not(any(target_os = "linux", target_os = "windows")))]
    fn shared_mapping_is_refused_with_a_clear_message_where_it_is_unimplemented() {
        // This used to cover Windows and does not any more: the plane is now
        // backed by a file mapping there.
        let err = PlaneMemory::create(std::path::Path::new("x"), 4096).unwrap_err();
        assert!(err.to_string().contains("Linux"));
    }

    #[test]
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    fn a_shared_plane_round_trips_through_the_filesystem() {
        // Live, on whichever platform is running. A consumer must be able to
        // find the plane by path and read back what the writer put there.
        let path = std::env::temp_dir().join(format!("corescout-plane-{}", std::process::id()));
        let _ = std::fs::remove_file(&path);
        {
            let mut writer = PlaneMemory::create(&path, 4096).expect("creatable");
            assert!(writer.is_writable());
            writer.as_mut_slice()[..4].copy_from_slice(&[1, 2, 3, 4]);
        }
        let reader = PlaneMemory::open_read_only(&path).expect("openable");
        assert!(
            !reader.is_writable(),
            "a consumer must not be able to write"
        );
        assert_eq!(&reader.as_slice()[..4], &[1, 2, 3, 4]);
        drop(reader);
        let _ = std::fs::remove_file(&path);
    }
}