Skip to content

Commit 86edc8f

Browse files
committed
Add more comments
1 parent 7709142 commit 86edc8f

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

src/shims/unix/linux/eventfd.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Linux `eventfd` implementation.
2-
//! Currently just a stub.
32
use std::io;
43
use std::io::{Error, ErrorKind};
54

@@ -41,7 +40,7 @@ impl FileDescription for Event {
4140
Ok(Ok(()))
4241
}
4342

44-
/// Read the counter in the buffer and return the counter if succeed.
43+
/// Read the counter in the buffer and return the counter if succeeded.
4544
fn read<'tcx>(
4645
&mut self,
4746
_communicate_allowed: bool,
@@ -52,6 +51,7 @@ impl FileDescription for Event {
5251
let Some(bytes) = bytes.first_chunk_mut::<8>() else {
5352
return Ok(Err(Error::from(ErrorKind::InvalidInput)));
5453
};
54+
// Block when counter == 0.
5555
if self.counter == 0 {
5656
if self.is_nonblock {
5757
return Ok(Err(Error::from(ErrorKind::WouldBlock)));
@@ -94,7 +94,7 @@ impl FileDescription for Event {
9494
let Some(bytes) = bytes.first_chunk::<8>() else {
9595
return Ok(Err(Error::from(ErrorKind::InvalidInput)));
9696
};
97-
// Convert from target endianness to host endianness.
97+
// Convert from bytes to int according to host endianness.
9898
let num = match ecx.tcx.sess.target.endian {
9999
Endian::Little => u64::from_le_bytes(*bytes),
100100
Endian::Big => u64::from_be_bytes(*bytes),
@@ -103,6 +103,8 @@ impl FileDescription for Event {
103103
if num == u64::MAX {
104104
return Ok(Err(Error::from(ErrorKind::InvalidInput)));
105105
}
106+
// If the addition does not let the counter to exceed the maximum value, update the counter.
107+
// Else, block.
106108
match self.counter.checked_add(num) {
107109
Some(new_count @ 0..=MAX_COUNTER) => {
108110
// Prevent false alarm in data race detection when doing synchronisation via eventfd.
@@ -166,7 +168,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
166168
flags &= !efd_nonblock;
167169
is_nonblock = true;
168170
}
169-
// After unloading, if flags != 0, that means other flags are used.
171+
// After unloading, flags != 0 means other flags are used.
170172
if flags != 0 {
171173
let einval = this.eval_libc("EINVAL");
172174
this.set_last_error(einval)?;

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ fn test_read_write() {
1515
let fd = unsafe { libc::eventfd(0, flags) };
1616
let sized_8_data: [u8; 8];
1717
if cfg!(target_endian = "big") {
18-
// Adjust the data based on the endianess of host system.
19-
sized_8_data = [0, 0, 0, 0, 0, 0, 0, 1];
18+
// Adjust the data based on the endianness of host system.
19+
sized_8_data = 1_i64.to_be_bytes();
2020
} else {
21-
sized_8_data = [1, 0, 0, 0, 0, 0, 0, 0];
21+
sized_8_data = 1_i64.to_le_bytes();
2222
}
2323
// Write 1 to the counter.
2424
let res: i64 = unsafe {
@@ -33,6 +33,7 @@ fn test_read_write() {
3333
};
3434
// Read returns number of bytes has been read, which is always 8.
3535
assert_eq!(res, 8);
36+
// Check the value of counter read.
3637
let counter: u64;
3738
if cfg!(target_endian = "big") {
3839
// Read will store the bytes based on the endianess of the host system.
@@ -42,47 +43,43 @@ fn test_read_write() {
4243
}
4344
assert_eq!(counter, 1);
4445

45-
// Read when counter == 0 should fail.
46+
// After read, the counter is currently 0, read counter 0 should fail with return
47+
// value -1.
4648
let mut buf: [u8; 8] = [0; 8];
4749
let res: i32 = unsafe {
4850
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
4951
};
50-
// Read returns number of bytes has been read, which is always 8.
5152
assert_eq!(res, -1);
5253

53-
// Write with buffer size > 8 bytes.
54+
// Write with supplied buffer that > 8 bytes should be allowed.
55+
let sized_9_data: [u8; 9];
56+
if cfg!(target_endian = "big") {
57+
// Adjust the data based on the endianess of host system.
58+
sized_9_data = [0, 0, 0, 0, 0, 0, 0, 0, 1];
59+
} else {
60+
sized_9_data = [1, 0, 0, 0, 0, 0, 0, 0, 0];
61+
}
5462
let res: i64 = unsafe {
55-
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
63+
libc::write(fd, sized_9_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
5664
};
5765
assert_eq!(res, 8);
5866

59-
// Read with supplied buffer that < 8 bytes.
67+
// Read with supplied buffer that < 8 bytes should fail with return
68+
// value -1.
6069
let mut buf: [u8; 7] = [1; 7];
6170
let res: i32 = unsafe {
6271
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
6372
};
6473
assert_eq!(res, -1);
6574

66-
// Write with supplied buffer that < 8 bytes
75+
// Write with supplied buffer that < 8 bytes should fail with return
76+
// value -1.
6777
let res: i64 = unsafe {
6878
libc::write(fd, sized_8_data[0..7].as_ptr() as *const libc::c_void, 7).try_into().unwrap()
6979
};
7080
assert_eq!(res, -1);
7181

72-
// Write with supplied buffer that > 8 bytes
73-
let sized_9_data: [u8; 9];
74-
if cfg!(target_endian = "big") {
75-
// Adjust the data based on the endianess of host system.
76-
sized_9_data = [0, 0, 0, 0, 0, 0, 0, 0, 1];
77-
} else {
78-
sized_9_data = [1, 0, 0, 0, 0, 0, 0, 0, 0];
79-
}
80-
let res: i64 = unsafe {
81-
libc::write(fd, sized_9_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
82-
};
83-
assert_eq!(res, 8);
84-
85-
// Read with supplied buffer > 8 bytes.
82+
// Read with supplied buffer > 8 bytes should be allowed.
8683
let mut buf: [u8; 9] = [1; 9];
8784
let res: i32 = unsafe {
8885
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()

0 commit comments

Comments
 (0)