Skip to content

Commit be30bcf

Browse files
committed
Rename helper function and apply comment suggestion
1 parent 575aa90 commit be30bcf

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

src/shims/unix/fd.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl FileDescription for io::Stdin {
142142
helpers::isolation_abort_error("`read` from stdin")?;
143143
}
144144
let result = Read::read(&mut { self }, &mut bytes);
145-
ecx.read_byte_helper(ptr, bytes, result, dest)?;
145+
ecx.return_read_bytes_and_count(ptr, bytes, result, dest)?;
146146
Ok(())
147147
}
148148

@@ -172,7 +172,7 @@ impl FileDescription for io::Stdout {
172172
// the host -- there is no good in adding extra buffering
173173
// here.
174174
io::stdout().flush().unwrap();
175-
ecx.write_byte_helper(result, dest)?;
175+
ecx.return_written_byte_count_or_error(result, dest)?;
176176
Ok(())
177177
}
178178

@@ -197,7 +197,7 @@ impl FileDescription for io::Stderr {
197197
// We allow writing to stderr even with isolation enabled.
198198
// No need to flush, stderr is not buffered.
199199
let result = Write::write(&mut { self }, bytes);
200-
ecx.write_byte_helper(result, dest)?;
200+
ecx.return_written_byte_count_or_error(result, dest)?;
201201
Ok(())
202202
}
203203

@@ -225,7 +225,7 @@ impl FileDescription for NullOutput {
225225
) -> InterpResult<'tcx> {
226226
// We just don't write anything, but report to the user that we did.
227227
let result = Ok(bytes.len());
228-
ecx.write_byte_helper(result, dest)?;
228+
ecx.return_written_byte_count_or_error(result, dest)?;
229229
Ok(())
230230
}
231231
}
@@ -644,8 +644,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
644644
Ok(())
645645
}
646646

647-
/// This function either writes to the user supplied buffer and to dest place, or return error.
648-
fn read_byte_helper(
647+
/// This function either writes to the user supplied buffer and to dest place, or sets the
648+
/// last libc error and writes -1 to dest.
649+
fn return_read_bytes_and_count(
649650
&mut self,
650651
buf: Pointer,
651652
bytes: Vec<u8>,
@@ -671,8 +672,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
671672
}
672673
}
673674

674-
/// This function either writes the number of written bytes to dest place or return error.
675-
fn write_byte_helper(
675+
/// This function writes the number of written bytes to dest place, or sets the
676+
/// last libc error and writes -1 to dest.
677+
fn return_written_byte_count_or_error(
676678
&mut self,
677679
result: io::Result<usize>,
678680
dest: &MPlaceTy<'tcx>,

src/shims/unix/fs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl FileDescription for FileHandle {
4242
assert!(communicate_allowed, "isolation should have prevented even opening a file");
4343
let mut bytes = vec![0; usize::try_from(len).unwrap()];
4444
let result = (&mut &self.file).read(&mut bytes);
45-
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
45+
ecx.return_read_bytes_and_count(ptr, bytes.to_vec(), result, dest)?;
4646
Ok(())
4747
}
4848

@@ -56,7 +56,7 @@ impl FileDescription for FileHandle {
5656
) -> InterpResult<'tcx> {
5757
assert!(communicate_allowed, "isolation should have prevented even opening a file");
5858
let result = (&mut &self.file).write(bytes);
59-
ecx.write_byte_helper(result, dest)?;
59+
ecx.return_written_byte_count_or_error(result, dest)?;
6060
Ok(())
6161
}
6262

@@ -85,7 +85,7 @@ impl FileDescription for FileHandle {
8585
res
8686
};
8787
let result = f();
88-
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
88+
ecx.return_read_bytes_and_count(ptr, bytes.to_vec(), result, dest)?;
8989
Ok(())
9090
}
9191

@@ -112,7 +112,7 @@ impl FileDescription for FileHandle {
112112
res
113113
};
114114
let result = f();
115-
ecx.write_byte_helper(result, dest)?;
115+
ecx.return_written_byte_count_or_error(result, dest)?;
116116
Ok(())
117117
}
118118

src/shims/unix/linux/eventfd.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl FileDescription for Event {
7272
// Check the size of slice, and return error only if the size of the slice < 8.
7373
if len < U64_ARRAY_SIZE.try_into().unwrap() {
7474
let result = Err(Error::from(ErrorKind::InvalidInput));
75-
read_byte_helper_ev(&buf_place, None, result, dest, ecx)?;
75+
return_read_bytes_and_count_ev(&buf_place, None, result, dest, ecx)?;
7676
return Ok(());
7777
}
7878

@@ -81,7 +81,7 @@ impl FileDescription for Event {
8181
if counter == 0 {
8282
if self.is_nonblock {
8383
let result = Err(Error::from(ErrorKind::WouldBlock));
84-
read_byte_helper_ev(&buf_place, None, result, dest, ecx)?;
84+
return_read_bytes_and_count_ev(&buf_place, None, result, dest, ecx)?;
8585
return Ok(());
8686
} else {
8787
//FIXME: blocking is not supported
@@ -91,7 +91,7 @@ impl FileDescription for Event {
9191
// Synchronize with all prior `write` calls to this FD.
9292
ecx.acquire_clock(&self.clock.borrow());
9393
let result = Ok(U64_ARRAY_SIZE);
94-
read_byte_helper_ev(&buf_place, Some(counter), result, dest, ecx)?;
94+
return_read_bytes_and_count_ev(&buf_place, Some(counter), result, dest, ecx)?;
9595
self.counter.set(0);
9696
// When any of the event happened, we check and update the status of all supported event
9797
// types for current file description.
@@ -124,7 +124,7 @@ impl FileDescription for Event {
124124
// Check the size of slice, and return error only if the size of the slice < 8.
125125
let Some(bytes) = bytes.first_chunk::<U64_ARRAY_SIZE>() else {
126126
let result = Err(Error::from(ErrorKind::InvalidInput));
127-
ecx.write_byte_helper(result, dest)?;
127+
ecx.return_written_byte_count_or_error(result, dest)?;
128128
return Ok(());
129129
};
130130
// Convert from bytes to int according to host endianness.
@@ -135,7 +135,7 @@ impl FileDescription for Event {
135135
// u64::MAX as input is invalid because the maximum value of counter is u64::MAX - 1.
136136
if num == u64::MAX {
137137
let result = Err(Error::from(ErrorKind::InvalidInput));
138-
ecx.write_byte_helper(result, dest)?;
138+
ecx.return_written_byte_count_or_error(result, dest)?;
139139
return Ok(());
140140
}
141141
// If the addition does not let the counter to exceed the maximum value, update the counter.
@@ -151,7 +151,7 @@ impl FileDescription for Event {
151151
None | Some(u64::MAX) => {
152152
if self.is_nonblock {
153153
let result = Err(Error::from(ErrorKind::WouldBlock));
154-
ecx.write_byte_helper(result, dest)?;
154+
ecx.return_written_byte_count_or_error(result, dest)?;
155155
return Ok(());
156156
} else {
157157
//FIXME: blocking is not supported
@@ -164,7 +164,7 @@ impl FileDescription for Event {
164164
ecx.check_and_update_readiness(self_ref)?;
165165

166166
let result = Ok(U64_ARRAY_SIZE);
167-
ecx.write_byte_helper(result, dest)?;
167+
ecx.return_written_byte_count_or_error(result, dest)?;
168168
Ok(())
169169
}
170170
}
@@ -231,8 +231,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
231231
}
232232
}
233233

234-
/// This function either writes to the user supplied buffer and to dest place, or return error.
235-
fn read_byte_helper_ev<'tcx>(
234+
/// This function either writes to the user supplied buffer and to dest place, or sets the
235+
/// last libc error and writes -1 to dest. This is only used by eventfd.
236+
fn return_read_bytes_and_count_ev<'tcx>(
236237
buf_place: &MPlaceTy<'tcx>,
237238
read_val: Option<u64>,
238239
result: io::Result<usize>,

src/shims/unix/unnamed_socket.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl FileDescription for AnonSocket {
137137
// Always succeed on read size 0.
138138
if request_byte_size == 0 {
139139
let result = Ok(0);
140-
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
140+
ecx.return_read_bytes_and_count(ptr, bytes.to_vec(), result, dest)?;
141141
return Ok(());
142142
}
143143

@@ -152,7 +152,7 @@ impl FileDescription for AnonSocket {
152152
// Socketpair with no peer and empty buffer.
153153
// 0 bytes successfully read indicates end-of-file.
154154
let result = Ok(0);
155-
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
155+
ecx.return_read_bytes_and_count(ptr, bytes.to_vec(), result, dest)?;
156156
return Ok(());
157157
} else {
158158
if self.is_nonblock {
@@ -162,7 +162,7 @@ impl FileDescription for AnonSocket {
162162
// POSIX.1-2001 allows either error to be returned for this case.
163163
// Since there is no ErrorKind for EAGAIN, WouldBlock is used.
164164
let result = Err(Error::from(ErrorKind::WouldBlock));
165-
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
165+
ecx.return_read_bytes_and_count(ptr, bytes.to_vec(), result, dest)?;
166166
return Ok(());
167167
} else {
168168
// Blocking socketpair with writer and empty buffer.
@@ -196,7 +196,7 @@ impl FileDescription for AnonSocket {
196196
}
197197

198198
let result = Ok(actual_read_size);
199-
ecx.read_byte_helper(ptr, bytes.to_vec(), result, dest)?;
199+
ecx.return_read_bytes_and_count(ptr, bytes.to_vec(), result, dest)?;
200200
return Ok(());
201201
}
202202

@@ -213,7 +213,7 @@ impl FileDescription for AnonSocket {
213213
// ("If count is zero and fd refers to a file other than a regular file, the results are not specified.")
214214
if write_size == 0 {
215215
let result = Ok(0);
216-
ecx.write_byte_helper(result, dest)?;
216+
ecx.return_written_byte_count_or_error(result, dest)?;
217217
return Ok(());
218218
}
219219

@@ -222,7 +222,7 @@ impl FileDescription for AnonSocket {
222222
// If the upgrade from Weak to Rc fails, it indicates that all read ends have been
223223
// closed.
224224
let result = Err(Error::from(ErrorKind::BrokenPipe));
225-
ecx.write_byte_helper(result, dest)?;
225+
ecx.return_written_byte_count_or_error(result, dest)?;
226226
return Ok(());
227227
};
228228

@@ -238,7 +238,7 @@ impl FileDescription for AnonSocket {
238238
if self.is_nonblock {
239239
// Non-blocking socketpair with a full buffer.
240240
let result = Err(Error::from(ErrorKind::WouldBlock));
241-
ecx.write_byte_helper(result, dest)?;
241+
ecx.return_written_byte_count_or_error(result, dest)?;
242242
return Ok(());
243243
} else {
244244
// Blocking socketpair with a full buffer.
@@ -261,7 +261,7 @@ impl FileDescription for AnonSocket {
261261
ecx.check_and_update_readiness(&peer_fd)?;
262262

263263
let result = Ok(actual_write_size);
264-
ecx.write_byte_helper(result, dest)?;
264+
ecx.return_written_byte_count_or_error(result, dest)?;
265265
return Ok(());
266266
}
267267
}

0 commit comments

Comments
 (0)