forkheap 0.1.0

An allocator suitable for sharing heaps between forked processes on Linux.
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
#![feature(allocator_api)]

use std::alloc::{AllocError, Allocator, Layout};
use std::cell::UnsafeCell;
use std::ffi::c_void;
use std::fs::{File, OpenOptions};
use std::io;
use std::io::ErrorKind;
use std::mem::MaybeUninit;
use std::os::fd::AsRawFd;
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;
use std::ptr::NonNull;
use std::thread::panicking;

use libc::{
    mmap, munmap, off_t, MAP_ANONYMOUS, MAP_FIXED, MAP_PRIVATE, MAP_SHARED, O_TMPFILE, PROT_NONE,
    PROT_READ, PROT_WRITE,
};
use linked_list_allocator::Heap;
pub use spin::Mutex;

const INITIAL_SIZE: usize = 4 << 20; // 4 MB

/// Trait which denotes that an allocator may be synchronised across forks.
pub trait SynchronizableAllocator {
    /// Perform synchronisation to ensure that the heap currently represents what is present across forks.
    /// You will not need to do this manually unless handling raw pointers in the synchronized heap.
    fn sync(&self) -> Result<(), AllocError>;
}

/// Metadata stored within the shared memory region regarding the heap.
struct HeapMetadata {
    /// Base address of the shared memory region.
    reserved: *mut c_void,
    /// Current size of the shared memory region.
    memsize: UnsafeCell<usize>,
    /// The heap, guarded by a spinlock mutex.
    heap: Mutex<Heap>,
}

/// A file-backed allocator for use in multiprocess programs or with very large allocations. Concurrent heap access is
/// managed by a spinlock mutex.
pub struct FileAllocator {
    /// The file which backs this allocator.
    file: File,
    /// The total size of the heap permitted.
    total: usize,
    /// The metadata of the heap as stored within the file.
    meta: &'static mut HeapMetadata,
    /// The current known size of the file. When this does not match the heap metadata, we are out of sync.
    /// Access to this field must be blocked by the mutex in the heap metadata.
    filesize: UnsafeCell<usize>,
}

impl FileAllocator {
    /// Create a new file-backed allocator with a given max total file size. The path provided must be a directory on
    /// a writable filesystem.
    pub fn new<P: AsRef<Path>>(within: P, total: usize) -> Result<Self, io::Error> {
        let file = OpenOptions::new()
            .custom_flags(O_TMPFILE)
            .write(true)
            .read(true)
            .open(within)?;

        file.set_len(INITIAL_SIZE as u64)?;

        // reserve the max potential heap size
        let reserved = unsafe {
            mmap(
                core::ptr::null_mut(),
                total,
                PROT_NONE,
                MAP_ANONYMOUS | MAP_PRIVATE,
                -1,
                0,
            )
        };
        if !reserved.is_null() {
            let base = unsafe {
                mmap(
                    reserved,
                    INITIAL_SIZE,
                    PROT_READ | PROT_WRITE,
                    MAP_FIXED | MAP_SHARED,
                    file.as_raw_fd(),
                    0,
                ) as *mut u8
            };
            if base as usize != usize::MAX {
                let base = base as *mut MaybeUninit<HeapMetadata>;
                let meta = unsafe { base.as_mut() }.unwrap();

                let heap_base = unsafe { base.offset(1) as *mut u8 };
                let size = INITIAL_SIZE - unsafe { heap_base.byte_offset_from(base) as usize };

                let heap = unsafe { Heap::new(heap_base, size) };

                let meta = meta.write(HeapMetadata {
                    reserved,
                    memsize: UnsafeCell::new(INITIAL_SIZE),
                    heap: Mutex::new(heap),
                });

                let result = Self {
                    file,
                    total,
                    meta,
                    filesize: UnsafeCell::new(INITIAL_SIZE),
                };
                return Ok(result);
            }
        }
        Err(io::Error::new(
            ErrorKind::AddrNotAvailable,
            io::Error::last_os_error(),
        ))
    }

    fn sync_unlocked(&self) -> Result<(), AllocError> {
        let heap_filesize = unsafe { *self.meta.memsize.get() };
        let my_filesize = unsafe { self.filesize.get().as_mut().unwrap() };
        if *my_filesize != heap_filesize {
            let v = unsafe {
                mmap(
                    self.meta.reserved,
                    heap_filesize,
                    PROT_READ | PROT_WRITE,
                    MAP_FIXED | MAP_SHARED,
                    self.file.as_raw_fd(),
                    0,
                )
            } as isize;
            if v.is_negative() || v != self.meta.reserved as isize {
                return Err(AllocError);
            }
            *my_filesize = heap_filesize;
        }
        Ok(())
    }
}

impl SynchronizableAllocator for FileAllocator {
    fn sync(&self) -> Result<(), AllocError> {
        let heap = self.meta.heap.lock();
        self.sync_unlocked()?;
        drop(heap);
        Ok(())
    }
}

impl Drop for FileAllocator {
    fn drop(&mut self) {
        if !panicking() {
            assert_eq!(self.meta.heap.lock().used(), 0); // must allocate beforehand!
            if unsafe { munmap(self.meta.reserved, self.total) } < 0 {
                panic!("munmap failed: {}", io::Error::last_os_error())
            }
        }
    }
}

unsafe impl Allocator for FileAllocator {
    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
        let mut heap = self.meta.heap.lock();
        self.sync_unlocked()?;

        let mut allocation = heap.allocate_first_fit(layout);
        while allocation.is_err() {
            let filesize = unsafe { self.meta.memsize.get().as_mut() }
                .expect("contract with UnsafeCell violated");
            let old_filesize = *filesize;
            if old_filesize == self.total {
                return Err(AllocError);
            }
            let new_filesize = (old_filesize * 2).min(self.total);
            let extension = new_filesize - old_filesize;
            *filesize = new_filesize;

            if unsafe { libc::fallocate(self.file.as_raw_fd(), 0, 0, new_filesize as off_t) } < 0 {
                let e = io::Error::last_os_error();
                match e.kind() {
                    ErrorKind::OutOfMemory => return Err(AllocError),
                    _ => panic!("unrecoverable io error: {e}"),
                }
            }
            self.sync_unlocked()?;
            unsafe {
                heap.extend(extension);
            }
            allocation = heap.allocate_first_fit(layout);
        }
        allocation
            .map(|ptr| NonNull::slice_from_raw_parts(ptr, layout.size()))
            .map_err(|_| AllocError)
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
        let mut heap = self.meta.heap.lock();
        heap.deallocate(ptr, layout);
    }
}

/// Box-like container for data in a shared heap that is accessed concurrently by multiple forks.
///
/// An Arc-like container is not provided, as counters would not be updated over [`libc::fork`].
pub struct ForkBox<'a, T, A>
where
    T: ?Sized,
{
    inner: *mut T,
    allocator: &'a A,
}

impl<'a, T, A> ForkBox<'a, T, A>
where
    T: ?Sized,
    A: Allocator + SynchronizableAllocator,
{
    /// Get a reference to the data. You are responsible for maintaining data safety!
    ///
    /// # Safety
    ///
    /// Use like [`UnsafeCell`]. This should be safe with inline spinlock mutexes, but other mutexes may have undefined
    /// behaviour.
    pub unsafe fn get(&self) -> Result<&'a T, AllocError> {
        self.allocator.sync()?;
        Ok(self.inner.as_mut().expect("invalid pointer provided"))
    }

    /// Get a mutable reference to the data. You are responsible for maintaining data safety!
    ///
    /// # Safety
    ///
    /// Use like [`UnsafeCell`]. Accessing mutably requires you to ensure that no other process currently is accessing
    /// the data.
    pub unsafe fn get_mut(&self) -> Result<&'a mut T, AllocError> {
        self.allocator.sync()?;
        Ok(self.inner.as_mut().expect("invalid pointer provided"))
    }

    /// Capture and own the data. You are responsible for maintaining data safety!
    ///
    /// # Safety
    ///
    /// Use like [`UnsafeCell`]. Taking ownership requires you to ensure that no other process currently is accessing
    /// the data.
    pub unsafe fn into_inner(self) -> Result<Box<T, &'a A>, AllocError> {
        self.allocator.sync()?;
        Ok(Box::from_raw_in(self.inner, self.allocator))
    }
}

impl<'a, T, A> From<Box<T, &'a A>> for ForkBox<'a, T, A>
where
    T: ?Sized,
    A: Allocator + SynchronizableAllocator,
{
    fn from(value: Box<T, &'a A>) -> Self {
        let (inner, allocator) = Box::into_raw_with_allocator(value);
        Self { inner, allocator }
    }
}

#[cfg(test)]
mod test {
    use std::{io, iter};

    use libc::{_exit, fork, waitpid};

    use crate::{FileAllocator, ForkBox};

    const WITHIN: &str = ".";

    #[test]
    fn basic_vec() -> Result<(), io::Error> {
        let allocator = FileAllocator::new(WITHIN, 32 << 30)?; // allow up to 32GB
        {
            let mut vec = Vec::new_in(&allocator);

            vec.extend_from_slice(&[0u8; 32]);
            for _ in 0..32 {
                vec.extend(iter::repeat(0u8).take(8 << 20)); // allocate 8MB
            }
        }

        Ok(())
    }

    fn many_vec_inner(allocator: &FileAllocator) {
        let mut vec = Vec::new();

        // allocate 4GB in 1MB chunks
        for _ in 0usize..(4 << 10) {
            let next = Vec::<u8, _>::with_capacity_in(1 << 20, allocator); // 1MB
            vec.push(next);
        }
    }

    #[test]
    fn many_vec() -> Result<(), io::Error> {
        let allocator = FileAllocator::new(WITHIN, 32 << 30)?; // allow up to 32GB
        many_vec_inner(&allocator);

        Ok(())
    }

    #[test]
    fn many_vec_dealloc() -> Result<(), io::Error> {
        let allocator = FileAllocator::new(WITHIN, 32 << 30)?; // allow up to 32GB
        for _ in 0usize..8 {
            many_vec_inner(&allocator);
        }

        Ok(())
    }

    #[test]
    fn fork_sanity_simple() -> Result<(), io::Error> {
        let allocator = FileAllocator::new(WITHIN, 32 << 30)?; // allow up to 32GB
        assert_eq!(allocator.meta.heap.lock().used(), 0);
        {
            let vec = Box::new_in(Vec::new_in(&allocator), &allocator);
            let vec = ForkBox::from(vec);
            assert_ne!(allocator.meta.heap.lock().used(), 0);

            match unsafe { fork() } {
                -1 => {
                    panic!("fork failed: {}", io::Error::last_os_error())
                }
                0 => {
                    // we are baby :)
                    let vec = unsafe { vec.get_mut() }.unwrap();

                    for _ in 0..32 {
                        vec.extend(iter::repeat(1u8).take(8 << 20)); // allocate 8MB
                    }

                    unsafe {
                        _exit(0);
                    }
                }
                pid => {
                    // we are parent
                    if unsafe { waitpid(pid, core::ptr::null_mut(), 0) } < 0 {
                        panic!("wait failed: {}", io::Error::last_os_error())
                    }
                    // the child process has ended, which means we can safely access the vec again
                    let vec = unsafe { vec.into_inner() }.unwrap();

                    assert_eq!(vec.len(), 32 * (8 << 20));
                    let heap_before = allocator.meta.heap.lock().used();
                    assert!(vec.into_iter().all(|e| e == 1));
                    assert!(allocator.meta.heap.lock().used() < heap_before);
                }
            }
        }

        Ok(())
    }

    #[test]
    fn fork_sanity_chaos() -> Result<(), io::Error> {
        const ALLOCATIONS: usize = 1 << 10;

        let allocator = FileAllocator::new(WITHIN, 32 << 30)?; // allow up to 32GB
        assert_eq!(allocator.meta.heap.lock().used(), 0);
        {
            let vec = Box::new_in(Vec::new_in(&allocator), &allocator);
            let vec = ForkBox::from(vec);
            assert_ne!(allocator.meta.heap.lock().used(), 0);

            match unsafe { fork() } {
                -1 => {
                    panic!("fork failed: {}", io::Error::last_os_error())
                }
                0 => {
                    // we are baby :)
                    let vec = unsafe { vec.get_mut() }.unwrap();

                    for _ in 0..32 {
                        vec.extend(iter::repeat(Box::new_in(1u8, &allocator)).take(ALLOCATIONS));
                    }

                    unsafe {
                        _exit(0);
                    }
                }
                pid => {
                    // we are parent
                    let mut intermediary = Vec::new_in(&allocator);

                    for _ in 0..32 {
                        intermediary
                            .extend(iter::repeat(Box::new_in(1u8, &allocator)).take(ALLOCATIONS));
                    }

                    if unsafe { waitpid(pid, core::ptr::null_mut(), 0) } < 0 {
                        panic!("wait failed: {}", io::Error::last_os_error())
                    }
                    // the child process has ended, which means we can safely access the vec again
                    let mut vec = unsafe { vec.into_inner() }.unwrap();

                    vec.extend(intermediary);

                    assert_eq!(vec.len(), 2 * 32 * (ALLOCATIONS));
                    let heap_before = allocator.meta.heap.lock().used();
                    assert!(vec.into_iter().all(|e| *e == 1));
                    assert!(allocator.meta.heap.lock().used() < heap_before);
                }
            }
        }

        Ok(())
    }
}