Skip to content

Commit 4f8613e

Browse files
committed
s/MiriSafeFd/MockableFd/
The need for this type isn't specific to Miri; it is necessary on toolchains containing rust-lang/rust#124210 - it just so happens that today this is nightly only, and so is Miri.
1 parent f448b3a commit 4f8613e

File tree

10 files changed

+99
-85
lines changed

10 files changed

+99
-85
lines changed

aya/src/lib.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -97,71 +97,79 @@ pub use object::Endianness;
9797
pub use sys::netlink_set_link_up;
9898

9999
// See https://github.com/rust-lang/rust/pull/124210; this structure exists to avoid crashing the
100-
// process when we try to close a fake file descriptor in Miri.
100+
// process when we try to close a fake file descriptor.
101101
#[derive(Debug)]
102-
struct MiriSafeFd {
103-
#[cfg(not(miri))]
102+
struct MockableFd {
103+
#[cfg(not(test))]
104104
fd: OwnedFd,
105-
#[cfg(miri)]
105+
#[cfg(test)]
106106
fd: Option<OwnedFd>,
107107
}
108108

109-
impl MiriSafeFd {
110-
#[cfg(any(test, miri))]
111-
const MOCK_FD: u16 = 1337;
109+
impl MockableFd {
110+
#[cfg(test)]
111+
const fn mock_signed_fd() -> i32 {
112+
1337
113+
}
114+
115+
#[cfg(test)]
116+
const fn mock_unsigned_fd() -> u32 {
117+
1337
118+
}
112119

113-
#[cfg(not(miri))]
120+
#[cfg(not(test))]
114121
fn from_fd(fd: OwnedFd) -> Self {
115122
Self { fd }
116123
}
117124

118-
#[cfg(miri)]
125+
#[cfg(test)]
119126
fn from_fd(fd: OwnedFd) -> Self {
120127
Self { fd: Some(fd) }
121128
}
122129

123-
#[cfg(not(miri))]
130+
#[cfg(not(test))]
124131
fn try_clone(&self) -> std::io::Result<Self> {
125132
let Self { fd } = self;
126133
let fd = fd.try_clone()?;
127134
Ok(Self { fd })
128135
}
129136

130-
#[cfg(miri)]
137+
#[cfg(test)]
131138
fn try_clone(&self) -> std::io::Result<Self> {
132139
let Self { fd } = self;
133140
let fd = fd.as_ref().map(OwnedFd::try_clone).transpose()?;
134141
Ok(Self { fd })
135142
}
136143
}
137144

138-
impl AsFd for MiriSafeFd {
139-
#[cfg(not(miri))]
145+
impl AsFd for MockableFd {
146+
#[cfg(not(test))]
140147
fn as_fd(&self) -> BorrowedFd<'_> {
141148
let Self { fd } = self;
142149
fd.as_fd()
143150
}
144151

145-
#[cfg(miri)]
152+
#[cfg(test)]
146153
fn as_fd(&self) -> BorrowedFd<'_> {
147154
let Self { fd } = self;
148155
fd.as_ref().unwrap().as_fd()
149156
}
150157
}
151158

152-
impl Drop for MiriSafeFd {
153-
#[cfg(not(miri))]
159+
impl Drop for MockableFd {
160+
#[cfg(not(test))]
154161
fn drop(&mut self) {
155162
// Intentional no-op.
156163
}
157164

158-
#[cfg(miri)]
165+
#[cfg(test)]
159166
fn drop(&mut self) {
160167
use std::os::fd::AsRawFd as _;
161168

162169
let Self { fd } = self;
163-
let fd = fd.take().unwrap();
164-
assert_eq!(fd.as_raw_fd(), Self::MOCK_FD.into());
165-
std::mem::forget(fd)
170+
if fd.as_ref().unwrap().as_raw_fd() >= Self::mock_signed_fd() {
171+
let fd: OwnedFd = fd.take().unwrap();
172+
std::mem::forget(fd)
173+
}
166174
}
167175
}

aya/src/maps/bloom_filter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<T: BorrowMut<MapData>, V: Pod> BloomFilter<T, V> {
7979

8080
#[cfg(test)]
8181
mod tests {
82-
use std::{ffi::c_long, io};
82+
use std::io;
8383

8484
use assert_matches::assert_matches;
8585
use libc::{EFAULT, ENOENT};
@@ -102,7 +102,7 @@ mod tests {
102102
test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_BLOOM_FILTER)
103103
}
104104

105-
fn sys_error(value: i32) -> SysResult<c_long> {
105+
fn sys_error(value: i32) -> SysResult<i64> {
106106
Err((-1, io::Error::from_raw_os_error(value)))
107107
}
108108

aya/src/maps/hash_map/hash_map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V>
103103

104104
#[cfg(test)]
105105
mod tests {
106-
use std::{ffi::c_long, io};
106+
use std::io;
107107

108108
use assert_matches::assert_matches;
109109
use libc::{EFAULT, ENOENT};
@@ -126,7 +126,7 @@ mod tests {
126126
test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_HASH)
127127
}
128128

129-
fn sys_error(value: i32) -> SysResult<c_long> {
129+
fn sys_error(value: i32) -> SysResult<i64> {
130130
Err((-1, io::Error::from_raw_os_error(value)))
131131
}
132132

@@ -332,7 +332,7 @@ mod tests {
332332
assert_matches!(keys, Ok(ks) if ks.is_empty())
333333
}
334334

335-
fn get_next_key(attr: &bpf_attr) -> SysResult<c_long> {
335+
fn get_next_key(attr: &bpf_attr) -> SysResult<i64> {
336336
match bpf_key(attr) {
337337
None => set_next_key(attr, 10),
338338
Some(10) => set_next_key(attr, 20),
@@ -344,7 +344,7 @@ mod tests {
344344
Ok(1)
345345
}
346346

347-
fn lookup_elem(attr: &bpf_attr) -> SysResult<c_long> {
347+
fn lookup_elem(attr: &bpf_attr) -> SysResult<i64> {
348348
match bpf_key(attr) {
349349
Some(10) => set_ret(attr, 100),
350350
Some(20) => set_ret(attr, 200),

aya/src/maps/lpm_trie.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<Key<K>, V> for LpmTrie<T, K
196196

197197
#[cfg(test)]
198198
mod tests {
199-
use std::{ffi::c_long, io, net::Ipv4Addr};
199+
use std::{io, net::Ipv4Addr};
200200

201201
use assert_matches::assert_matches;
202202
use libc::{EFAULT, ENOENT};
@@ -219,7 +219,7 @@ mod tests {
219219
test_utils::new_obj_map::<Key<u32>>(BPF_MAP_TYPE_LPM_TRIE)
220220
}
221221

222-
fn sys_error(value: i32) -> SysResult<c_long> {
222+
fn sys_error(value: i32) -> SysResult<i64> {
223223
Err((-1, io::Error::from_raw_os_error(value)))
224224
}
225225

aya/src/maps/mod.rs

+38-24
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ impl From<InvalidMapTypeError> for MapError {
211211
/// A map file descriptor.
212212
#[derive(Debug)]
213213
pub struct MapFd {
214-
fd: crate::MiriSafeFd,
214+
fd: crate::MockableFd,
215215
}
216216

217217
impl MapFd {
218218
fn from_fd(fd: OwnedFd) -> Self {
219-
let fd = crate::MiriSafeFd::from_fd(fd);
219+
let fd = crate::MockableFd::from_fd(fd);
220220
Self { fd }
221221
}
222222

@@ -1052,7 +1052,7 @@ mod test_utils {
10521052
Syscall::Ebpf {
10531053
cmd: bpf_cmd::BPF_MAP_CREATE,
10541054
..
1055-
} => Ok(crate::MiriSafeFd::MOCK_FD.into()),
1055+
} => Ok(crate::MockableFd::mock_signed_fd().into()),
10561056
call => panic!("unexpected syscall {:?}", call),
10571057
});
10581058
MapData::create(obj, "foo", None).unwrap()
@@ -1103,15 +1103,15 @@ mod tests {
11031103
unsafe { attr.__bindgen_anon_6.__bindgen_anon_1.map_id },
11041104
1234
11051105
);
1106-
Ok(crate::MiriSafeFd::MOCK_FD.into())
1106+
Ok(crate::MockableFd::mock_signed_fd().into())
11071107
}
11081108
Syscall::Ebpf {
11091109
cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD,
11101110
attr,
11111111
} => {
11121112
assert_eq!(
11131113
unsafe { attr.info.bpf_fd },
1114-
crate::MiriSafeFd::MOCK_FD.into()
1114+
crate::MockableFd::mock_unsigned_fd(),
11151115
);
11161116
Ok(0)
11171117
}
@@ -1123,7 +1123,7 @@ mod tests {
11231123
Ok(MapData {
11241124
obj: _,
11251125
fd,
1126-
}) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MiriSafeFd::MOCK_FD.into())
1126+
}) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MockableFd::mock_signed_fd())
11271127
);
11281128
}
11291129

@@ -1133,7 +1133,7 @@ mod tests {
11331133
Syscall::Ebpf {
11341134
cmd: bpf_cmd::BPF_MAP_CREATE,
11351135
..
1136-
} => Ok(crate::MiriSafeFd::MOCK_FD.into()),
1136+
} => Ok(crate::MockableFd::mock_signed_fd().into()),
11371137
_ => Err((-1, io::Error::from_raw_os_error(EFAULT))),
11381138
});
11391139

@@ -1142,7 +1142,7 @@ mod tests {
11421142
Ok(MapData {
11431143
obj: _,
11441144
fd,
1145-
}) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MiriSafeFd::MOCK_FD.into())
1145+
}) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MockableFd::mock_signed_fd())
11461146
);
11471147
}
11481148

@@ -1160,7 +1160,7 @@ mod tests {
11601160
Syscall::Ebpf {
11611161
cmd: bpf_cmd::BPF_MAP_CREATE,
11621162
..
1163-
} => Ok(42),
1163+
} => Ok(crate::MockableFd::mock_signed_fd().into()),
11641164
Syscall::Ebpf {
11651165
cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD,
11661166
attr,
@@ -1206,13 +1206,15 @@ mod tests {
12061206
Syscall::Ebpf {
12071207
cmd: bpf_cmd::BPF_MAP_GET_FD_BY_ID,
12081208
attr,
1209-
} => Ok((1000 + unsafe { attr.__bindgen_anon_6.__bindgen_anon_1.map_id }) as c_long),
1209+
} => Ok((unsafe { attr.__bindgen_anon_6.__bindgen_anon_1.map_id }
1210+
+ crate::MockableFd::mock_unsigned_fd())
1211+
.into()),
12101212
Syscall::Ebpf {
12111213
cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD,
12121214
attr,
12131215
} => {
12141216
let map_info = unsafe { &mut *(attr.info.info as *mut bpf_map_info) };
1215-
map_info.id = unsafe { attr.info.bpf_fd } - 1000;
1217+
map_info.id = unsafe { attr.info.bpf_fd } - crate::MockableFd::mock_unsigned_fd();
12161218
map_info.key_size = 32;
12171219
map_info.value_size = 64;
12181220
map_info.map_flags = 1234;
@@ -1222,19 +1224,31 @@ mod tests {
12221224
_ => Err((-1, io::Error::from_raw_os_error(EFAULT))),
12231225
});
12241226

1225-
let loaded_maps: Vec<_> = loaded_maps().collect();
1226-
assert_eq!(loaded_maps.len(), 5);
1227-
1228-
for (i, map_info) in loaded_maps.into_iter().enumerate() {
1229-
let i = i + 1;
1230-
let map_info = map_info.unwrap();
1231-
assert_eq!(map_info.id(), i as u32);
1232-
assert_eq!(map_info.key_size(), 32);
1233-
assert_eq!(map_info.value_size(), 64);
1234-
assert_eq!(map_info.map_flags(), 1234);
1235-
assert_eq!(map_info.max_entries(), 99);
1236-
assert_eq!(map_info.fd().unwrap().as_fd().as_raw_fd(), 1000 + i as i32);
1237-
}
1227+
assert_eq!(
1228+
loaded_maps()
1229+
.map(|map_info| {
1230+
let map_info = map_info.unwrap();
1231+
(
1232+
map_info.id(),
1233+
map_info.key_size(),
1234+
map_info.value_size(),
1235+
map_info.map_flags(),
1236+
map_info.max_entries(),
1237+
map_info.fd().unwrap().as_fd().as_raw_fd(),
1238+
)
1239+
})
1240+
.collect::<Vec<_>>(),
1241+
(1..6)
1242+
.map(|i: u8| (
1243+
i.into(),
1244+
32,
1245+
64,
1246+
1234,
1247+
99,
1248+
crate::MockableFd::mock_signed_fd() + i32::from(i)
1249+
))
1250+
.collect::<Vec<_>>(),
1251+
);
12381252
}
12391253

12401254
#[test]

aya/src/maps/perf/perf_buffer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub(crate) struct PerfBuffer {
8888
buf: AtomicPtr<perf_event_mmap_page>,
8989
size: usize,
9090
page_size: usize,
91-
fd: crate::MiriSafeFd,
91+
fd: crate::MockableFd,
9292
}
9393

9494
impl PerfBuffer {
@@ -120,7 +120,7 @@ impl PerfBuffer {
120120
});
121121
}
122122

123-
let fd = crate::MiriSafeFd::from_fd(fd);
123+
let fd = crate::MockableFd::from_fd(fd);
124124
let perf_buf = Self {
125125
buf: AtomicPtr::new(buf as *mut perf_event_mmap_page),
126126
size,
@@ -303,7 +303,7 @@ mod tests {
303303
fn fake_mmap(buf: &MMappedBuf) {
304304
override_syscall(|call| match call {
305305
Syscall::PerfEventOpen { .. } | Syscall::PerfEventIoctl { .. } => {
306-
Ok(crate::MiriSafeFd::MOCK_FD.into())
306+
Ok(crate::MockableFd::mock_signed_fd().into())
307307
}
308308
call => panic!("unexpected syscall: {:?}", call),
309309
});

0 commit comments

Comments
 (0)