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
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! FileReadWriteAtVolatile is implemented in terms of `pread` and `pwrite`. These are provided by
//! platform-specific imports. On Linux they resolve to the 64-bit versions, while on MacOS the
//! base versions are already 64-bit.

use std::fs::File;
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixStream;

use crate::FileReadWriteAtVolatile;
use crate::FileReadWriteVolatile;

// This module allows the below macros to refer to $crate::unix::file_traits::lib::X and ensures
// other crates don't need to add additional crates to their Cargo.toml.
pub mod lib {
    pub use libc::c_int;
    pub use libc::c_void;
    pub use libc::iovec;
    pub use libc::read;
    pub use libc::readv;
    pub use libc::size_t;
    pub use libc::write;
    pub use libc::writev;
}

#[macro_export]
macro_rules! volatile_impl {
    ($ty:ty) => {
        impl FileReadWriteVolatile for $ty {
            fn read_volatile(&mut self, slice: $crate::VolatileSlice) -> std::io::Result<usize> {
                // SAFETY:
                // Safe because only bytes inside the slice are accessed and the kernel is expected
                // to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::unix::file_traits::lib::read(
                        self.as_raw_fd(),
                        slice.as_mut_ptr() as *mut std::ffi::c_void,
                        slice.size() as usize,
                    )
                };
                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }

            fn read_vectored_volatile(
                &mut self,
                bufs: &[$crate::VolatileSlice],
            ) -> std::io::Result<usize> {
                let iobufs = $crate::VolatileSlice::as_iobufs(bufs);
                let iovecs = $crate::IoBufMut::as_iobufs(iobufs);

                if iovecs.is_empty() {
                    return Ok(0);
                }

                // SAFETY:
                // Safe because only bytes inside the buffers are accessed and the kernel is
                // expected to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::unix::file_traits::lib::readv(
                        self.as_raw_fd(),
                        iovecs.as_ptr(),
                        iovecs.len() as std::os::raw::c_int,
                    )
                };
                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }

            fn write_volatile(&mut self, slice: $crate::VolatileSlice) -> std::io::Result<usize> {
                // SAFETY:
                // Safe because only bytes inside the slice are accessed and the kernel is expected
                // to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::unix::file_traits::lib::write(
                        self.as_raw_fd(),
                        slice.as_ptr() as *const std::ffi::c_void,
                        slice.size() as usize,
                    )
                };
                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }

            fn write_vectored_volatile(
                &mut self,
                bufs: &[$crate::VolatileSlice],
            ) -> std::io::Result<usize> {
                let iobufs = $crate::VolatileSlice::as_iobufs(bufs);
                let iovecs = $crate::IoBufMut::as_iobufs(iobufs);

                if iovecs.is_empty() {
                    return Ok(0);
                }

                // SAFETY:
                // Safe because only bytes inside the buffers are accessed and the kernel is
                // expected to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::unix::file_traits::lib::writev(
                        self.as_raw_fd(),
                        iovecs.as_ptr(),
                        iovecs.len() as std::os::raw::c_int,
                    )
                };
                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }
        }
    };
}

#[macro_export]
macro_rules! volatile_at_impl {
    ($ty:ty) => {
        impl FileReadWriteAtVolatile for $ty {
            fn read_at_volatile(
                &self,
                slice: $crate::VolatileSlice,
                offset: u64,
            ) -> std::io::Result<usize> {
                // SAFETY:
                // Safe because only bytes inside the slice are accessed and the kernel is expected
                // to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::platform::pread(
                        self.as_raw_fd(),
                        slice.as_mut_ptr() as *mut std::ffi::c_void,
                        slice.size() as usize,
                        offset as $crate::platform::off_t,
                    )
                };

                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }

            fn read_vectored_at_volatile(
                &self,
                bufs: &[$crate::VolatileSlice],
                offset: u64,
            ) -> std::io::Result<usize> {
                let iobufs = $crate::VolatileSlice::as_iobufs(bufs);
                let iovecs = $crate::IoBufMut::as_iobufs(iobufs);

                if iovecs.is_empty() {
                    return Ok(0);
                }

                // SAFETY:
                // Safe because only bytes inside the buffers are accessed and the kernel is
                // expected to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::platform::preadv(
                        self.as_raw_fd(),
                        iovecs.as_ptr(),
                        iovecs.len() as std::os::raw::c_int,
                        offset as $crate::platform::off_t,
                    )
                };
                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }

            fn write_at_volatile(
                &self,
                slice: $crate::VolatileSlice,
                offset: u64,
            ) -> std::io::Result<usize> {
                // SAFETY:
                // Safe because only bytes inside the slice are accessed and the kernel is expected
                // to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::platform::pwrite(
                        self.as_raw_fd(),
                        slice.as_ptr() as *const std::ffi::c_void,
                        slice.size() as usize,
                        offset as $crate::platform::off_t,
                    )
                };

                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }

            fn write_vectored_at_volatile(
                &self,
                bufs: &[$crate::VolatileSlice],
                offset: u64,
            ) -> std::io::Result<usize> {
                let iobufs = $crate::VolatileSlice::as_iobufs(bufs);
                let iovecs = $crate::IoBufMut::as_iobufs(iobufs);

                if iovecs.is_empty() {
                    return Ok(0);
                }

                // SAFETY:
                // Safe because only bytes inside the buffers are accessed and the kernel is
                // expected to handle arbitrary memory for I/O.
                let ret = unsafe {
                    $crate::platform::pwritev(
                        self.as_raw_fd(),
                        iovecs.as_ptr(),
                        iovecs.len() as std::os::raw::c_int,
                        offset as $crate::platform::off_t,
                    )
                };
                if ret >= 0 {
                    Ok(ret as usize)
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }
        }
    };
}

volatile_impl!(File);
volatile_at_impl!(File);
volatile_impl!(UnixStream);