Skip to content

Commit 8c7d6e5

Browse files
committed
Resolve conflict
1 parent b8c40c4 commit 8c7d6e5

File tree

5 files changed

+62
-72
lines changed

5 files changed

+62
-72
lines changed

Diff for: src/shims/unix/fd.rs

+42-9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub trait FileDescription: std::fmt::Debug + Any {
5151

5252
fn close<'tcx>(
5353
self: Box<Self>,
54+
_ecx: &mut MiriInterpCx<'tcx>,
5455
_communicate_allowed: bool,
5556
) -> InterpResult<'tcx, io::Result<()>> {
5657
throw_unsup_format!("cannot close {}", self.name());
@@ -197,11 +198,15 @@ impl FileDescriptor {
197198
RefMut::map(self.0.borrow_mut(), |fd| fd.as_mut())
198199
}
199200

200-
pub fn close<'ctx>(self, communicate_allowed: bool) -> InterpResult<'ctx, io::Result<()>> {
201+
pub fn close<'tcx>(
202+
self,
203+
ecx: &mut MiriInterpCx<'tcx>,
204+
communicate_allowed: bool,
205+
) -> InterpResult<'tcx, io::Result<()>> {
201206
// Destroy this `Rc` using `into_inner` so we can call `close` instead of
202207
// implicitly running the destructor of the file description.
203208
match Rc::into_inner(self.0) {
204-
Some(fd) => RefCell::into_inner(fd).close(communicate_allowed),
209+
Some(fd) => RefCell::into_inner(fd).close(ecx, communicate_allowed),
205210
None => Ok(Ok(())),
206211
}
207212
}
@@ -414,13 +419,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
414419

415420
let fd = this.read_scalar(fd_op)?.to_i32()?;
416421

417-
let Some(file_descriptor) = this.machine.fds.remove(fd) else {
418-
return Ok(Scalar::from_i32(this.fd_not_found()?));
419-
};
420-
let result = file_descriptor.close(this.machine.communicate())?;
421-
// return `0` if close is successful
422-
let result = result.map(|()| 0i32);
423-
Ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
422+
Ok(Scalar::from_i32(if let Some(file_descriptor) = this.machine.fds.remove(fd) {
423+
let result = file_descriptor.close(this, this.machine.communicate())?;
424+
// return `0` if close is successful
425+
let result = result.map(|()| 0i32);
426+
this.try_unwrap_io_result(result)?
427+
} else {
428+
this.fd_not_found()?
429+
}))
424430
}
425431

426432
/// Function used when a file descriptor does not exist. It returns `Ok(-1)`and sets
@@ -434,6 +440,33 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
434440
Ok((-1).into())
435441
}
436442

443+
/// Functions to update the ready list of an epoll_event.
444+
fn update_readiness(
445+
&self,
446+
ready_flags: Vec<u32>,
447+
epoll_events: &Vec<Weak<EpollEvent>>,
448+
) -> InterpResult<'tcx> {
449+
for event in epoll_events {
450+
// If the current epoll_event contains the flag that is signaled as ready, we add/update
451+
// the epoll_return entry in the ready list.
452+
if let Some(epoll_event) = event.upgrade() {
453+
if let Some(flags) = epoll_event.contains_flag(&ready_flags) {
454+
let weak_file_descriptor = epoll_event.weak_file_descriptor.clone();
455+
let epoll_key = (weak_file_descriptor, epoll_event.file_descriptor);
456+
// Retrieve the epoll return if it is already in the return list.
457+
let ready_list = &mut epoll_event.ready_list.borrow_mut();
458+
// Add a new epoll entry if it doesn't exist, or update the event mask if it exists.
459+
let epoll_return = EpollReturn::new(flags, epoll_event.data);
460+
let epoll_entry = ready_list.entry(epoll_key).or_insert(epoll_return);
461+
// The update here is bitwise or, so it will still be correct if we try to add
462+
// a flag that already exists.
463+
epoll_entry.update_events(flags);
464+
}
465+
}
466+
}
467+
Ok(())
468+
}
469+
437470
fn read(&mut self, fd: i32, buf: Pointer, count: u64) -> InterpResult<'tcx, i64> {
438471
let this = self.eval_context_mut();
439472

Diff for: src/shims/unix/fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl FileDescription for FileHandle {
6060

6161
fn close<'tcx>(
6262
self: Box<Self>,
63+
_ecx: &mut MiriInterpCx<'tcx>,
6364
communicate_allowed: bool,
6465
) -> InterpResult<'tcx, io::Result<()>> {
6566
assert!(communicate_allowed, "isolation should have prevented even opening a file");

Diff for: src/shims/unix/linux/epoll.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl FileDescription for Epoll {
8888

8989
fn close<'tcx>(
9090
self: Box<Self>,
91+
_ecx: &mut MiriInterpCx<'tcx>,
9192
_communicate_allowed: bool,
9293
) -> InterpResult<'tcx, io::Result<()>> {
9394
Ok(Ok(()))
@@ -214,11 +215,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
214215
interest_list.insert(epoll_key, event);
215216

216217
target_file_description.check_readiness(this).map(|events| {
217-
target_file_description.update_readiness(
218-
events,
219-
target_file_description.return_epoll_events().unwrap(),
220-
)
221-
})?;
218+
this.update_readiness(events, target_file_description.return_epoll_events()?)
219+
})??;
222220

223221
Ok(Scalar::from_i32(0))
224222
} else if op == epoll_ctl_del {

Diff for: src/shims/unix/linux/eventfd.rs

+5-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::rc::Weak;
66

77
use rustc_target::abi::Endian;
88

9-
use crate::shims::unix::linux::epoll::{EpollEvent, EpollReturn};
9+
use crate::shims::unix::linux::epoll::EpollEvent;
1010
use crate::shims::unix::*;
1111
use crate::{concurrency::VClock, *};
1212

@@ -35,30 +35,6 @@ struct Event {
3535
epoll_events: Vec<Weak<EpollEvent>>,
3636
}
3737

38-
impl Event {
39-
/// Functions to update the ready list of an epoll_event.
40-
fn update_readiness(&self, ready_flags: Vec<u32>, epoll_events: &Vec<Weak<EpollEvent>>) {
41-
for event in epoll_events {
42-
// If the current epoll_event contains the flag that is signaled as ready, we add/update
43-
// the epoll_return entry in the ready list.
44-
if let Some(epoll_event) = event.upgrade() {
45-
if let Some(flags) = epoll_event.contains_flag(&ready_flags) {
46-
let weak_file_descriptor = epoll_event.weak_file_descriptor.clone();
47-
let epoll_key = (weak_file_descriptor, epoll_event.file_descriptor);
48-
// Retrieve the epoll return if it is already in the return list.
49-
let ready_list = &mut epoll_event.ready_list.borrow_mut();
50-
// Add a new epoll entry if it doesn't exist, or update the event mask if it exists.
51-
let epoll_return = EpollReturn::new(flags, epoll_event.data);
52-
let epoll_entry = ready_list.entry(epoll_key).or_insert(epoll_return);
53-
// The update here is bitwise or, so it will still be correct if we try to add
54-
// a flag that already exists.
55-
epoll_entry.update_events(flags);
56-
}
57-
}
58-
}
59-
}
60-
}
61-
6238
impl FileDescription for Event {
6339
fn name(&self) -> &'static str {
6440
"event"
@@ -86,6 +62,7 @@ impl FileDescription for Event {
8662
}
8763
fn close<'tcx>(
8864
self: Box<Self>,
65+
_ecx: &mut MiriInterpCx<'tcx>,
8966
_communicate_allowed: bool,
9067
) -> InterpResult<'tcx, io::Result<()>> {
9168
Ok(Ok(()))
@@ -121,7 +98,7 @@ impl FileDescription for Event {
12198
self.counter = 0;
12299
// When any of the event is happened, we check and update the status of all supported flags.
123100
self.check_readiness(ecx)
124-
.map(|events| self.update_readiness(events, self.return_epoll_events().unwrap()))?;
101+
.map(|events| ecx.update_readiness(events, self.return_epoll_events()?))??;
125102
return Ok(Ok(U64_ARRAY_SIZE));
126103
}
127104
}
@@ -167,9 +144,8 @@ impl FileDescription for Event {
167144
}
168145
self.counter = new_count;
169146
// When any of the event is happened, we check and update the status of all supported flags.
170-
self.check_readiness(ecx).map(|events| {
171-
self.update_readiness(events, self.return_epoll_events().unwrap())
172-
})?;
147+
self.check_readiness(ecx)
148+
.map(|events| ecx.update_readiness(events, self.return_epoll_events()?))??;
173149
}
174150
None | Some(u64::MAX) => {
175151
if self.is_nonblock {

Diff for: src/shims/unix/socket.rs

+11-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::io::{Error, ErrorKind, Read};
55
use std::rc::{Rc, Weak};
66

77
use crate::shims::unix::fd::WeakFileDescriptor;
8-
use crate::shims::unix::linux::epoll::EpollReturn;
98
use crate::shims::unix::*;
109
use crate::{concurrency::VClock, *};
1110

@@ -38,28 +37,6 @@ impl SocketPair {
3837
fn peer_is_closed(&mut self) {
3938
self.peer_closed = true;
4039
}
41-
42-
/// Functions to update the ready list of an epoll_event.
43-
fn update_readiness(&self, ready_flags: Vec<u32>, epoll_events: &Vec<Weak<EpollEvent>>) {
44-
for event in epoll_events {
45-
// If the current epoll_event contains the flag that is signaled as ready, we add/update
46-
// the epoll_return entry in the ready list.
47-
if let Some(epoll_event) = event.upgrade() {
48-
if let Some(flags) = epoll_event.contains_flag(&ready_flags) {
49-
let weak_file_descriptor = epoll_event.weak_file_descriptor.clone();
50-
let epoll_key = (weak_file_descriptor, epoll_event.file_descriptor);
51-
// Retrieve the epoll return if it is already in the return list.
52-
let ready_list = &mut epoll_event.ready_list.borrow_mut();
53-
// Add a new epoll entry if it doesn't exist, or update the event mask if it exists.
54-
let epoll_return = EpollReturn::new(flags, epoll_event.data);
55-
let epoll_entry = ready_list.entry(epoll_key).or_insert(epoll_return);
56-
// The update here is bitwise or, so it will still be correct if we try to add
57-
// a flag that already exists.
58-
epoll_entry.update_events(flags);
59-
}
60-
}
61-
}
62-
}
6340
}
6441

6542
#[derive(Debug)]
@@ -109,11 +86,17 @@ impl FileDescription for SocketPair {
10986
}
11087
}
11188

89+
// Check if the peer_fd closed
90+
if self.peer_closed {
91+
ready_flags.push(epollrdhup);
92+
}
93+
11294
Ok(ready_flags)
11395
}
11496

11597
fn close<'tcx>(
11698
self: Box<Self>,
99+
ecx: &mut MiriInterpCx<'tcx>,
117100
_communicate_allowed: bool,
118101
) -> InterpResult<'tcx, io::Result<()>> {
119102
// This is used to signal socketfd of other side that there is no writer to its readbuf.
@@ -129,12 +112,11 @@ impl FileDescription for SocketPair {
129112
let peer_socketpair = binding.downcast_mut::<SocketPair>().unwrap();
130113
peer_socketpair.peer_is_closed();
131114
// When any of the event is happened, we check and update the status of all supported flags.
132-
//peer_socketpair
133-
// .check_readiness(ecx)
134-
// .map(|events| ecx.update_readiness(events, self.return_epoll_events()?))??;
115+
peer_socketpair
116+
.check_readiness(ecx)
117+
.map(|events| ecx.update_readiness(events, self.return_epoll_events()?))??;
135118
}
136119
}
137-
// TODO: invoke check readiness from other side
138120
Ok(Ok(()))
139121
}
140122

@@ -182,7 +164,7 @@ impl FileDescription for SocketPair {
182164
let actual_read_size = readbuf.buf.read(bytes).unwrap();
183165
// When any of the event is happened, we check and update the status of all supported flags.
184166
self.check_readiness(ecx)
185-
.map(|events| self.update_readiness(events, self.return_epoll_events().unwrap()))?;
167+
.map(|events| ecx.update_readiness(events, self.return_epoll_events()?))??;
186168
return Ok(Ok(actual_read_size));
187169
}
188170

@@ -225,7 +207,7 @@ impl FileDescription for SocketPair {
225207
writebuf.buf.extend(&bytes[..actual_write_size]);
226208
// When any of the event is happened, we check and update the status of all supported flags.
227209
self.check_readiness(ecx)
228-
.map(|events| self.update_readiness(events, self.return_epoll_events().unwrap()))?;
210+
.map(|events| ecx.update_readiness(events, self.return_epoll_events()?))??;
229211
return Ok(Ok(actual_write_size));
230212
}
231213
}

0 commit comments

Comments
 (0)