Skip to content

Commit a080f8c

Browse files
committed
Add basic tests
1 parent ee1d77a commit a080f8c

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/shims/unix/linux/eventfd.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,10 @@ impl FileDescription for Event {
4848
bytes: &mut [u8],
4949
ecx: &mut MiriInterpCx<'tcx>,
5050
) -> InterpResult<'tcx, io::Result<usize>> {
51-
// Use bytes[0..8] so it won't fail when bytes's size > 8.
52-
let mut buffer = match bytes[0..8].try_into() {
53-
Ok(bytes) => bytes,
54-
Err(_) => {
55-
// Size of bytes is less than 8.
56-
return Ok(Err(Error::from(ErrorKind::InvalidInput)));
57-
}
58-
};
51+
// Check the size of slice, and return error only if the size of the slice < 8.
52+
if <[u8; 8]>::try_from(&bytes[0..8]).is_err() {
53+
return Ok(Err(Error::from(ErrorKind::InvalidInput)));
54+
}
5955
if self.counter == 0 {
6056
if self.is_nonblock {
6157
return Ok(Err(Error::from(ErrorKind::WouldBlock)));
@@ -65,13 +61,12 @@ impl FileDescription for Event {
6561
}
6662
} else {
6763
// Return the counter in the host endianness using the buffer provided by caller.
68-
//TODO: check if why mutable bytes is not used? is this not the same buffer?
69-
buffer = match ecx.tcx.sess.target.endian {
64+
let counter_byte: [u8; 8] = match ecx.tcx.sess.target.endian {
7065
Endian::Little => self.counter.to_le_bytes(),
7166
Endian::Big => self.counter.to_be_bytes(),
7267
};
68+
bytes.copy_from_slice(&counter_byte);
7369
self.counter = 0;
74-
// It is always 8 bytes being read.
7570
return Ok(Ok(8));
7671
}
7772
}
@@ -94,7 +89,7 @@ impl FileDescription for Event {
9489
bytes: &[u8],
9590
ecx: &mut MiriInterpCx<'tcx>,
9691
) -> InterpResult<'tcx, io::Result<usize>> {
97-
let mut bytes = match bytes[0..8].try_into() {
92+
let bytes = match bytes[0..8].try_into() {
9893
Ok(bytes) => bytes,
9994
Err(_) => {
10095
// Size of bytes is less than 8.
@@ -121,7 +116,7 @@ impl FileDescription for Event {
121116
throw_unsup_format!("eventfd: blocking is unsupported");
122117
}
123118
} else {
124-
self.counter.checked_add(num).unwrap();
119+
self.counter = self.counter.checked_add(num).unwrap();
125120
}
126121
Ok(Ok(8))
127122
}

tests/pass-dep/libc/libc-eventfd.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ignore-target-windows: No eventfd in windows
2+
3+
fn main() {
4+
test_read_write();
5+
}
6+
7+
fn test_read_write() {
8+
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC;
9+
let fd = unsafe { libc::eventfd(0, flags) };
10+
let data: [u8; 8];
11+
if cfg!(target_endian = "big") {
12+
// Adjust the data based on the endianess of host system.
13+
data = [0, 0, 0, 0, 0, 0, 0, 1];
14+
} else {
15+
data = [1, 0, 0, 0, 0, 0, 0, 0];
16+
}
17+
// Write 1 to the counter
18+
let res: i64 =
19+
unsafe { libc::write(fd, data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() };
20+
assert_eq!(res, 8);
21+
22+
// Read 1 from the counter
23+
let mut buf: [u8; 8] = [1; 8];
24+
let res: i32 = unsafe {
25+
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
26+
};
27+
// read returns number of bytes has been read, which is always 8
28+
assert_eq!(res, 8);
29+
let counter: u64;
30+
if cfg!(target_endian = "big") {
31+
// Read will store the bytes based on the endianess of the host system.
32+
counter = u64::from_be_bytes(buf);
33+
} else {
34+
counter = u64::from_le_bytes(buf);
35+
}
36+
assert_eq!(counter, 1);
37+
}

0 commit comments

Comments
 (0)