Skip to main content

zx/
fifo.rs

1// Copyright 2017 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Type-safe bindings for Zircon fifo objects.
6
7use crate::{NullableHandle, Status, ok, sys};
8use std::mem::MaybeUninit;
9use zerocopy::{FromBytes, IntoBytes};
10
11/// An object representing a Zircon fifo.
12///
13/// As essentially a subtype of `NullableHandle`, it can be freely interconverted.
14///
15/// Encodes the element type in the type. Defaults to `()` for the entry type to allow for untyped
16/// IPC. Use `Fifo::cast()` to convert an IPC-transferred fifo to one of the specific type required
17/// that will support reads and writes.
18#[repr(transparent)]
19pub struct Fifo<R = UnspecifiedFifoElement, W = R>(
20    NullableHandle,
21    std::marker::PhantomData<(R, W)>,
22);
23
24impl_handle_based!(Fifo, [R, W], [R, W]);
25
26impl<R: IntoBytes + FromBytes, W: IntoBytes + FromBytes> Fifo<R, W> {
27    /// Create a pair of fifos and return their endpoints. Writing to one endpoint enqueues an
28    /// element into the fifo from which the opposing endpoint reads.
29    ///
30    /// Wraps the
31    /// [zx_fifo_create](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_create.md)
32    /// syscall.
33    pub fn create(elem_count: usize) -> Result<(Self, Fifo<W, R>), Status> {
34        if std::mem::size_of::<R>() != std::mem::size_of::<W>() {
35            return Err(Status::INVALID_ARGS);
36        }
37        let mut out0 = 0;
38        let mut out1 = 0;
39        let options = 0;
40
41        // SAFETY: this is a basic FFI call, and the mutable references are valid pointers.
42        let status = unsafe {
43            sys::zx_fifo_create(elem_count, std::mem::size_of::<R>(), options, &mut out0, &mut out1)
44        };
45        ok(status)?;
46
47        // SAFETY: if the above call succeeded, these are valid handle numbers.
48        unsafe {
49            Ok((
50                Fifo::from(NullableHandle::from_raw(out0)),
51                Fifo::from(NullableHandle::from_raw(out1)),
52            ))
53        }
54    }
55
56    /// Attempts to write some number of elements into the fifo. On success, returns the number of
57    /// elements actually written.
58    ///
59    /// Wraps
60    /// [zx_fifo_write](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_write.md).
61    pub fn write(&self, buf: &[W]) -> Result<usize, Status> {
62        // SAFETY: this pointer is valid for the length of the slice
63        unsafe { self.write_raw(buf.as_ptr(), buf.len()) }
64    }
65
66    /// Attempts to write a single element into the fifo.
67    ///
68    /// Wraps
69    /// [zx_fifo_write](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_write.md).
70    pub fn write_one(&self, elem: &W) -> Result<(), Status> {
71        // SAFETY: this pointer is valid for a single element
72        unsafe { self.write_raw(elem, 1).map(|n| debug_assert_eq!(n, 1)) }
73    }
74
75    /// Attempts to write some number of elements into the fifo. On success, returns the number of
76    /// elements actually written.
77    ///
78    /// Wraps
79    /// [zx_fifo_write](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_write.md).
80    ///
81    /// # Safety
82    ///
83    /// The caller is responsible for ensuring `buf` is valid to write to for `count` elements.
84    pub unsafe fn write_raw(&self, buf: *const W, count: usize) -> Result<usize, Status> {
85        let mut actual_count = 0;
86        // SAFETY: safety requirements for this call are upheld by our caller.
87        let status = unsafe {
88            sys::zx_fifo_write(
89                self.raw_handle(),
90                std::mem::size_of::<W>(),
91                buf.cast::<u8>(),
92                count,
93                &mut actual_count,
94            )
95        };
96        ok(status).map(|()| actual_count)
97    }
98
99    /// Attempts to read some elements out of the fifo. On success, returns the number of elements
100    /// actually read.
101    ///
102    /// Wraps
103    /// [zx_fifo_read](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_read.md).
104    pub fn read(&self, buf: &mut [R]) -> Result<usize, Status> {
105        // SAFETY: the pointer is valid for the length of the slice
106        unsafe { self.read_raw(buf.as_mut_ptr(), buf.len()) }
107    }
108
109    /// Attempts to read a single element out of the fifo.
110    ///
111    /// Wraps
112    /// [zx_fifo_read](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_read.md).
113    pub fn read_one(&self) -> Result<R, Status> {
114        let mut elem = MaybeUninit::uninit();
115
116        // SAFETY: the reference is valid to write to, and this call will not read from the bytes.
117        let valid_count = unsafe { self.read_raw(elem.as_mut_ptr(), 1)? };
118        debug_assert_eq!(valid_count, 1);
119
120        // SAFETY: if the previous call succeeded, the kernel has initialized this value.
121        Ok(unsafe { elem.assume_init() })
122    }
123
124    /// Attempts to read some number of elements out of the fifo. On success, returns a slice of
125    /// initialized elements.
126    ///
127    /// Wraps
128    /// [zx_fifo_read](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_read.md).
129    pub fn read_uninit<'a>(&self, bytes: &'a mut [MaybeUninit<R>]) -> Result<&'a mut [R], Status> {
130        // SAFETY: the slice is valid to write to for its entire length, and this call will not
131        // read from the bytes
132        let valid_count = unsafe { self.read_raw(bytes.as_mut_ptr().cast::<R>(), bytes.len())? };
133        let (valid, _uninit) = bytes.split_at_mut(valid_count);
134
135        // SAFETY: the kernel initialized all bytes, strip out MaybeUninit
136        unsafe { Ok(std::slice::from_raw_parts_mut(valid.as_mut_ptr().cast::<R>(), valid.len())) }
137    }
138
139    /// Attempts to read some number of elements out of the fifo. On success, returns the number of
140    /// elements actually read.
141    ///
142    /// Wraps
143    /// [zx_fifo_read](https://fuchsia.dev/fuchsia-src/reference/syscalls/fifo_read.md).
144    ///
145    /// # Safety
146    ///
147    /// The caller is responsible for ensuring `bytes` points to valid (albeit
148    /// not necessarily initialized) memory at least `len` bytes long.
149    pub unsafe fn read_raw(&self, buf: *mut R, count: usize) -> Result<usize, Status> {
150        let mut actual_count = 0;
151        // SAFETY: this call's invariants must be upheld by our caller.
152        let status = unsafe {
153            sys::zx_fifo_read(
154                self.raw_handle(),
155                std::mem::size_of::<R>(),
156                buf.cast::<u8>(),
157                count,
158                &mut actual_count,
159            )
160        };
161        ok(status).map(|()| actual_count)
162    }
163}
164
165impl Fifo<UnspecifiedFifoElement> {
166    /// Give a `Fifo` specific read/write types. The size of `R2` and `W2` must match
167    /// the element size the underlying handle was created with for reads and writes to succeed.
168    pub fn cast<R2, W2>(self) -> Fifo<R2, W2> {
169        Fifo::<R2, W2>::from(self.0)
170    }
171}
172
173impl<R, W> Fifo<R, W> {
174    /// Convert a fifo from having a specific element type to a fifo without any element type that
175    /// will not support reads or writes.
176    pub fn downcast(self) -> Fifo {
177        Fifo::from(self.0)
178    }
179}
180
181impl<R: FromBytes + IntoBytes, W: FromBytes + IntoBytes> From<Fifo> for Fifo<R, W> {
182    fn from(untyped: Fifo) -> Self {
183        untyped.cast()
184    }
185}
186
187impl<R, W> std::fmt::Debug for Fifo<R, W> {
188    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
189        let read_name = std::any::type_name::<R>();
190        let (_, short_read_name) = read_name.rsplit_once("::").unwrap();
191        let write_name = std::any::type_name::<W>();
192        let (_, short_write_name) = write_name.rsplit_once("::").unwrap();
193        f.debug_tuple(&format!("Fifo<{short_read_name}, {short_write_name}>"))
194            .field(&self.0)
195            .finish()
196    }
197}
198
199impl<R, W> std::cmp::PartialEq for Fifo<R, W> {
200    fn eq(&self, other: &Self) -> bool {
201        self.0 == other.0
202    }
203}
204impl<R, W> std::cmp::Eq for Fifo<R, W> {}
205
206impl<R, W> std::cmp::PartialOrd for Fifo<R, W> {
207    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
208        self.0.partial_cmp(&other.0)
209    }
210}
211impl<R, W> std::cmp::Ord for Fifo<R, W> {
212    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
213        self.0.cmp(&other.0)
214    }
215}
216
217impl<R, W> std::hash::Hash for Fifo<R, W> {
218    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
219        self.0.hash(state)
220    }
221}
222
223/// The default element for fifos, does not support reading or writing. Only used for IPC transfer.
224#[derive(Copy, Clone, Debug)]
225pub struct UnspecifiedFifoElement;
226
227#[cfg(test)]
228mod tests {
229    use super::*;
230
231    #[test]
232    fn fifo_basic() {
233        let (fifo1, fifo2) = Fifo::<[u8; 2]>::create(4).unwrap();
234
235        // Trying to write less than one element should fail.
236        assert_eq!(fifo1.write(&[]), Err(Status::OUT_OF_RANGE));
237
238        // Should write one element "he"
239        fifo1.write_one(b"he").unwrap();
240
241        // Should write three elements "ll" "o " "wo" and drop the rest as it is full.
242        assert_eq!(fifo1.write(&[*b"ll", *b"o ", *b"wo", *b"rl", *b"ds"]).unwrap(), 3);
243
244        // Now that the fifo is full any further attempts to write should fail.
245        assert_eq!(fifo1.write(&[*b"bl", *b"ah", *b"bl", *b"ah"]), Err(Status::SHOULD_WAIT));
246
247        assert_eq!(fifo2.read_one().unwrap(), *b"he");
248
249        // Read remaining 3 entries from the other end.
250        let mut read_vec = vec![[0; 2]; 8];
251        assert_eq!(fifo2.read(&mut read_vec).unwrap(), 3);
252        assert_eq!(read_vec, &[*b"ll", *b"o ", *b"wo", [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]);
253
254        // Reading again should fail as the fifo is empty.
255        assert_eq!(fifo2.read(&mut read_vec), Err(Status::SHOULD_WAIT));
256    }
257}