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
//! A buffer for files that need to share a buffer with other files. Example use-cases are pipes and
//! unix sockets. This buffer supports notifying files when readers or writers are added or removed.

use linux_api::errno::Errno;

use crate::utility::byte_queue::ByteQueue;
use crate::utility::callback_queue::{CallbackQueue, EventSource, Handle};

pub struct SharedBuf {
    queue: ByteQueue,
    max_len: usize,
    state: BufferState,
    num_readers: u16,
    num_writers: u16,
    event_source: EventSource<(BufferState, BufferState, BufferSignals)>,
}

impl SharedBuf {
    pub fn new(max_len: usize) -> Self {
        assert_ne!(max_len, 0);
        Self {
            queue: ByteQueue::new(4096),
            max_len,
            state: BufferState::WRITABLE | BufferState::NO_READERS | BufferState::NO_WRITERS,
            num_readers: 0,
            num_writers: 0,
            event_source: EventSource::new(),
        }
    }

    pub fn has_data(&self) -> bool {
        self.queue.has_chunks()
    }

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

    pub fn space_available(&self) -> usize {
        self.max_len - self.queue.num_bytes()
    }

    /// Register as a reader. The [`ReaderHandle`] must be returned to the buffer later with
    /// [`remove_reader()`](Self::remove_reader).
    pub fn add_reader(&mut self, cb_queue: &mut CallbackQueue) -> ReaderHandle {
        self.num_readers += 1;
        self.refresh_state(BufferSignals::empty(), cb_queue);
        ReaderHandle {}
    }

    pub fn remove_reader(&mut self, handle: ReaderHandle, cb_queue: &mut CallbackQueue) {
        self.num_readers -= 1;
        // don't run the handle's drop impl
        std::mem::forget(handle);
        self.refresh_state(BufferSignals::empty(), cb_queue);
    }

    pub fn num_readers(&self) -> u16 {
        self.num_readers
    }

    /// Register as a writer. The [`WriterHandle`] must be returned to the buffer later with
    /// [`remove_writer()`](Self::remove_writer).
    pub fn add_writer(&mut self, cb_queue: &mut CallbackQueue) -> WriterHandle {
        self.num_writers += 1;
        self.refresh_state(BufferSignals::empty(), cb_queue);
        WriterHandle {}
    }

    pub fn remove_writer(&mut self, handle: WriterHandle, cb_queue: &mut CallbackQueue) {
        self.num_writers -= 1;
        // don't run the handle's drop impl
        std::mem::forget(handle);
        self.refresh_state(BufferSignals::empty(), cb_queue);
    }

    pub fn num_writers(&self) -> u16 {
        self.num_writers
    }

    pub fn peek<W: std::io::Write>(&self, bytes: W) -> Result<(usize, usize), std::io::Error> {
        let (num_copied, num_removed_from_buf) = match self.queue.peek(bytes)? {
            Some((num_copied, num_removed_from_buf, _chunk_type)) => {
                (num_copied, num_removed_from_buf)
            }
            None => (0, 0),
        };
        Ok((num_copied, num_removed_from_buf))
    }

    pub fn read<W: std::io::Write>(
        &mut self,
        bytes: W,
        cb_queue: &mut CallbackQueue,
    ) -> Result<(usize, usize), std::io::Error> {
        let (num_copied, num_removed_from_buf) = match self.queue.pop(bytes)? {
            Some((num_copied, num_removed_from_buf, _chunk_type)) => {
                (num_copied, num_removed_from_buf)
            }
            None => (0, 0),
        };
        self.refresh_state(BufferSignals::empty(), cb_queue);

        Ok((num_copied, num_removed_from_buf))
    }

    pub fn write_stream<R: std::io::Read>(
        &mut self,
        bytes: R,
        len: usize,
        cb_queue: &mut CallbackQueue,
    ) -> Result<usize, std::io::Error> {
        if len == 0 {
            return Ok(0);
        }

        if self.space_available() == 0 {
            return Err(Errno::EAGAIN.into());
        }

        let written = self
            .queue
            .push_stream(bytes.take(self.space_available().try_into().unwrap()))?;

        let signals = if written > 0 {
            BufferSignals::BUFFER_GREW
        } else {
            BufferSignals::empty()
        };
        self.refresh_state(signals, cb_queue);

        Ok(written)
    }

    pub fn write_packet<R: std::io::Read>(
        &mut self,
        mut bytes: R,
        len: usize,
        cb_queue: &mut CallbackQueue,
    ) -> Result<(), std::io::Error> {
        if len > self.max_len() {
            // the socket could never send this packet, even if the buffer was empty
            return Err(Errno::EMSGSIZE.into());
        }

        if len > self.space_available() {
            return Err(Errno::EAGAIN.into());
        }

        self.queue.push_packet(bytes.by_ref(), len)?;

        self.refresh_state(BufferSignals::BUFFER_GREW, cb_queue);

        Ok(())
    }

    pub fn add_listener(
        &mut self,
        monitoring_state: BufferState,
        monitoring_signals: BufferSignals,
        notify_fn: impl Fn(BufferState, BufferSignals, &mut CallbackQueue) + Send + Sync + 'static,
    ) -> BufferHandle {
        self.event_source
            .add_listener(move |(state, changed, signals), cb_queue| {
                // true if any of the bits we're monitoring have changed
                let flipped = monitoring_state.intersects(changed);

                // filter the signals to only the ones we're monitoring
                let signals = signals.intersection(monitoring_signals);

                if !flipped && signals.is_empty() {
                    return;
                }

                (notify_fn)(state, signals, cb_queue)
            })
    }

    pub fn state(&self) -> BufferState {
        self.state
    }

    /// Refresh the shared buffer's state and optionally send any signals. These two functionalities
    /// are combined into a single method since a state change and signals can be emitted as a
    /// single event, improving performance.
    fn refresh_state(&mut self, signals: BufferSignals, cb_queue: &mut CallbackQueue) {
        let state_mask = BufferState::READABLE
            | BufferState::WRITABLE
            | BufferState::NO_READERS
            | BufferState::NO_WRITERS;

        let mut new_state = BufferState::empty();

        new_state.set(BufferState::READABLE, self.has_data());
        new_state.set(BufferState::WRITABLE, self.space_available() > 0);
        new_state.set(BufferState::NO_READERS, self.num_readers() == 0);
        new_state.set(BufferState::NO_WRITERS, self.num_writers() == 0);

        self.update_state(state_mask, new_state, signals, cb_queue);
    }

    fn update_state(
        &mut self,
        mask: BufferState,
        state: BufferState,
        signals: BufferSignals,
        cb_queue: &mut CallbackQueue,
    ) {
        let old_state = self.state;

        // remove the masked flags, then copy the masked flags
        self.state.remove(mask);
        self.state.insert(state & mask);

        self.handle_state_change(old_state, signals, cb_queue);
    }

    fn handle_state_change(
        &mut self,
        old_state: BufferState,
        signals: BufferSignals,
        cb_queue: &mut CallbackQueue,
    ) {
        let states_changed = self.state ^ old_state;

        // if nothing changed
        if states_changed.is_empty() && signals.is_empty() {
            return;
        }

        self.event_source
            .notify_listeners((self.state, states_changed, signals), cb_queue);
    }
}

impl Drop for SharedBuf {
    fn drop(&mut self) {
        // don't show the following warning message if panicking
        if std::thread::panicking() {
            return;
        }

        // listeners waiting for `NO_READERS` or `NO_WRITERS` status changes will never be notified
        if self.num_readers != 0 || self.num_writers != 0 {
            // panic in debug builds since the backtrace will be helpful for debugging
            debug_panic!(
                "Dropping SharedBuf while it still has {} readers and {} writers.",
                self.num_readers,
                self.num_writers,
            );
        }
    }
}

bitflags::bitflags! {
    #[derive(Default, Copy, Clone, Debug)]
    pub struct BufferState: u8 {
        /// There is data waiting in the buffer.
        const READABLE = 0b00000001;
        /// There is available buffer space.
        const WRITABLE = 0b00000010;
        /// The buffer has no readers.
        const NO_READERS = 0b00000100;
        /// The buffer has no writers.
        const NO_WRITERS = 0b00001000;
    }
}

bitflags::bitflags! {
    /// Buffer-related signals that listeners can watch for.
    #[derive(Default, Copy, Clone, Debug)]
    pub struct BufferSignals: u8 {
        /// The buffer now has additional data available to read.
        const BUFFER_GREW = 1 << 0;
    }
}

pub type BufferHandle = Handle<(BufferState, BufferState, BufferSignals)>;

/// A handle that signifies that the owner is acting as a reader for the buffer. The handle must be
/// returned to the buffer later with [`SharedBuf::remove_reader()`].
///
/// Handles aren't linked to specific buffers, so make sure to only return the handle to the same
/// buffer which you acquired the handle from.
// do not implement copy or clone
pub struct ReaderHandle;

/// See [`ReaderHandle`].
// do not implement copy or clone
pub struct WriterHandle;

impl Drop for ReaderHandle {
    fn drop(&mut self) {
        // don't show the following warning message if panicking
        if std::thread::panicking() {
            return;
        }

        // panic in debug builds since the backtrace will be helpful for debugging
        debug_panic!(
            "Dropping ReaderHandle without returning it to SharedBuf. \
             This likely indicates a bug in Shadow."
        );
    }
}

impl Drop for WriterHandle {
    fn drop(&mut self) {
        // don't show the following warning message if panicking
        if std::thread::panicking() {
            return;
        }

        // panic in debug builds since the backtrace will be helpful for debugging
        debug_panic!(
            "Dropping WriterHandle without returning it to SharedBuf. \
             This likely indicates a bug in Shadow."
        );
    }
}