Skip to content

Commit e6604ea

Browse files
committed
Auto merge of rust-lang#3867 - DeSevilla:rename-fd, r=RalfJung
Renamed variable and fixed comments referring to renamed FileDescriptor Fixes rust-lang#3808, does not change any functionality
2 parents 59cb24d + d4eeb31 commit e6604ea

File tree

3 files changed

+87
-96
lines changed

3 files changed

+87
-96
lines changed

src/tools/miri/src/shims/unix/fd.rs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(crate) enum FlockOp {
2121
Unlock,
2222
}
2323

24-
/// Represents an open file descriptor.
24+
/// Represents an open file description.
2525
pub trait FileDescription: std::fmt::Debug + Any {
2626
fn name(&self) -> &'static str;
2727

@@ -303,7 +303,7 @@ pub struct FdTable {
303303

304304
impl VisitProvenance for FdTable {
305305
fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
306-
// All our FileDescriptor do not have any tags.
306+
// All our FileDescription instances do not have any tags.
307307
}
308308
}
309309

@@ -337,18 +337,18 @@ impl FdTable {
337337
}
338338

339339
pub fn insert(&mut self, fd_ref: FileDescriptionRef) -> i32 {
340-
self.insert_ref_with_min_fd(fd_ref, 0)
340+
self.insert_with_min_num(fd_ref, 0)
341341
}
342342

343-
/// Insert a file description, giving it a file descriptor that is at least `min_fd`.
344-
fn insert_ref_with_min_fd(&mut self, file_handle: FileDescriptionRef, min_fd: i32) -> i32 {
343+
/// Insert a file description, giving it a file descriptor that is at least `min_fd_num`.
344+
fn insert_with_min_num(&mut self, file_handle: FileDescriptionRef, min_fd_num: i32) -> i32 {
345345
// Find the lowest unused FD, starting from min_fd. If the first such unused FD is in
346346
// between used FDs, the find_map combinator will return it. If the first such unused FD
347347
// is after all other used FDs, the find_map combinator will return None, and we will use
348348
// the FD following the greatest FD thus far.
349349
let candidate_new_fd =
350-
self.fds.range(min_fd..).zip(min_fd..).find_map(|((fd, _fh), counter)| {
351-
if *fd != counter {
350+
self.fds.range(min_fd_num..).zip(min_fd_num..).find_map(|((fd_num, _fd), counter)| {
351+
if *fd_num != counter {
352352
// There was a gap in the fds stored, return the first unused one
353353
// (note that this relies on BTreeMap iterating in key order)
354354
Some(counter)
@@ -357,61 +357,61 @@ impl FdTable {
357357
None
358358
}
359359
});
360-
let new_fd = candidate_new_fd.unwrap_or_else(|| {
360+
let new_fd_num = candidate_new_fd.unwrap_or_else(|| {
361361
// find_map ran out of BTreeMap entries before finding a free fd, use one plus the
362362
// maximum fd in the map
363-
self.fds.last_key_value().map(|(fd, _)| fd.strict_add(1)).unwrap_or(min_fd)
363+
self.fds.last_key_value().map(|(fd_num, _)| fd_num.strict_add(1)).unwrap_or(min_fd_num)
364364
});
365365

366-
self.fds.try_insert(new_fd, file_handle).unwrap();
367-
new_fd
366+
self.fds.try_insert(new_fd_num, file_handle).unwrap();
367+
new_fd_num
368368
}
369369

370-
pub fn get(&self, fd: i32) -> Option<FileDescriptionRef> {
371-
let fd = self.fds.get(&fd)?;
370+
pub fn get(&self, fd_num: i32) -> Option<FileDescriptionRef> {
371+
let fd = self.fds.get(&fd_num)?;
372372
Some(fd.clone())
373373
}
374374

375-
pub fn remove(&mut self, fd: i32) -> Option<FileDescriptionRef> {
376-
self.fds.remove(&fd)
375+
pub fn remove(&mut self, fd_num: i32) -> Option<FileDescriptionRef> {
376+
self.fds.remove(&fd_num)
377377
}
378378

379-
pub fn is_fd(&self, fd: i32) -> bool {
380-
self.fds.contains_key(&fd)
379+
pub fn is_fd_num(&self, fd_num: i32) -> bool {
380+
self.fds.contains_key(&fd_num)
381381
}
382382
}
383383

384384
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
385385
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
386-
fn dup(&mut self, old_fd: i32) -> InterpResult<'tcx, Scalar> {
386+
fn dup(&mut self, old_fd_num: i32) -> InterpResult<'tcx, Scalar> {
387387
let this = self.eval_context_mut();
388388

389-
let Some(dup_fd) = this.machine.fds.get(old_fd) else {
389+
let Some(fd) = this.machine.fds.get(old_fd_num) else {
390390
return Ok(Scalar::from_i32(this.fd_not_found()?));
391391
};
392-
Ok(Scalar::from_i32(this.machine.fds.insert_ref_with_min_fd(dup_fd, 0)))
392+
Ok(Scalar::from_i32(this.machine.fds.insert(fd)))
393393
}
394394

395-
fn dup2(&mut self, old_fd: i32, new_fd: i32) -> InterpResult<'tcx, Scalar> {
395+
fn dup2(&mut self, old_fd_num: i32, new_fd_num: i32) -> InterpResult<'tcx, Scalar> {
396396
let this = self.eval_context_mut();
397397

398-
let Some(dup_fd) = this.machine.fds.get(old_fd) else {
398+
let Some(fd) = this.machine.fds.get(old_fd_num) else {
399399
return Ok(Scalar::from_i32(this.fd_not_found()?));
400400
};
401-
if new_fd != old_fd {
401+
if new_fd_num != old_fd_num {
402402
// Close new_fd if it is previously opened.
403403
// If old_fd and new_fd point to the same description, then `dup_fd` ensures we keep the underlying file description alive.
404-
if let Some(file_description) = this.machine.fds.fds.insert(new_fd, dup_fd) {
404+
if let Some(old_new_fd) = this.machine.fds.fds.insert(new_fd_num, fd) {
405405
// Ignore close error (not interpreter's) according to dup2() doc.
406-
file_description.close(this.machine.communicate(), this)?.ok();
406+
old_new_fd.close(this.machine.communicate(), this)?.ok();
407407
}
408408
}
409-
Ok(Scalar::from_i32(new_fd))
409+
Ok(Scalar::from_i32(new_fd_num))
410410
}
411411

412-
fn flock(&mut self, fd: i32, op: i32) -> InterpResult<'tcx, Scalar> {
412+
fn flock(&mut self, fd_num: i32, op: i32) -> InterpResult<'tcx, Scalar> {
413413
let this = self.eval_context_mut();
414-
let Some(file_descriptor) = this.machine.fds.get(fd) else {
414+
let Some(fd) = this.machine.fds.get(fd_num) else {
415415
return Ok(Scalar::from_i32(this.fd_not_found()?));
416416
};
417417

@@ -436,8 +436,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
436436
throw_unsup_format!("unsupported flags {:#x}", op);
437437
};
438438

439-
let result = file_descriptor.flock(this.machine.communicate(), parsed_op)?;
440-
drop(file_descriptor);
439+
let result = fd.flock(this.machine.communicate(), parsed_op)?;
440+
drop(fd);
441441
// return `0` if flock is successful
442442
let result = result.map(|()| 0i32);
443443
Ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
@@ -452,7 +452,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
452452
args.len()
453453
);
454454
}
455-
let fd = this.read_scalar(&args[0])?.to_i32()?;
455+
let fd_num = this.read_scalar(&args[0])?.to_i32()?;
456456
let cmd = this.read_scalar(&args[1])?.to_i32()?;
457457

458458
// We only support getting the flags for a descriptor.
@@ -461,7 +461,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
461461
// `FD_CLOEXEC` value without checking if the flag is set for the file because `std`
462462
// always sets this flag when opening a file. However we still need to check that the
463463
// file itself is open.
464-
Ok(Scalar::from_i32(if this.machine.fds.is_fd(fd) {
464+
Ok(Scalar::from_i32(if this.machine.fds.is_fd_num(fd_num) {
465465
this.eval_libc_i32("FD_CLOEXEC")
466466
} else {
467467
this.fd_not_found()?
@@ -481,9 +481,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
481481
}
482482
let start = this.read_scalar(&args[2])?.to_i32()?;
483483

484-
match this.machine.fds.get(fd) {
485-
Some(dup_fd) =>
486-
Ok(Scalar::from_i32(this.machine.fds.insert_ref_with_min_fd(dup_fd, start))),
484+
match this.machine.fds.get(fd_num) {
485+
Some(fd) => Ok(Scalar::from_i32(this.machine.fds.insert_with_min_num(fd, start))),
487486
None => Ok(Scalar::from_i32(this.fd_not_found()?)),
488487
}
489488
} else if this.tcx.sess.target.os == "macos" && cmd == this.eval_libc_i32("F_FULLFSYNC") {
@@ -494,7 +493,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
494493
return Ok(Scalar::from_i32(-1));
495494
}
496495

497-
this.ffullsync_fd(fd)
496+
this.ffullsync_fd(fd_num)
498497
} else {
499498
throw_unsup_format!("the {:#x} command is not supported for `fcntl`)", cmd);
500499
}
@@ -503,12 +502,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
503502
fn close(&mut self, fd_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
504503
let this = self.eval_context_mut();
505504

506-
let fd = this.read_scalar(fd_op)?.to_i32()?;
505+
let fd_num = this.read_scalar(fd_op)?.to_i32()?;
507506

508-
let Some(file_description) = this.machine.fds.remove(fd) else {
507+
let Some(fd) = this.machine.fds.remove(fd_num) else {
509508
return Ok(Scalar::from_i32(this.fd_not_found()?));
510509
};
511-
let result = file_description.close(this.machine.communicate(), this)?;
510+
let result = fd.close(this.machine.communicate(), this)?;
512511
// return `0` if close is successful
513512
let result = result.map(|()| 0i32);
514513
Ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
@@ -532,16 +531,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
532531
/// and keeps the cursor unchanged.
533532
fn read(
534533
&mut self,
535-
fd: i32,
534+
fd_num: i32,
536535
buf: Pointer,
537536
count: u64,
538537
offset: Option<i128>,
539538
) -> InterpResult<'tcx, Scalar> {
540539
let this = self.eval_context_mut();
541540

542-
// Isolation check is done via `FileDescriptor` trait.
541+
// Isolation check is done via `FileDescription` trait.
543542

544-
trace!("Reading from FD {}, size {}", fd, count);
543+
trace!("Reading from FD {}, size {}", fd_num, count);
545544

546545
// Check that the *entire* buffer is actually valid memory.
547546
this.check_ptr_access(buf, Size::from_bytes(count), CheckInAllocMsg::MemoryAccessTest)?;
@@ -554,7 +553,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
554553
let communicate = this.machine.communicate();
555554

556555
// We temporarily dup the FD to be able to retain mutable access to `this`.
557-
let Some(fd) = this.machine.fds.get(fd) else {
556+
let Some(fd) = this.machine.fds.get(fd_num) else {
558557
trace!("read: FD not found");
559558
return Ok(Scalar::from_target_isize(this.fd_not_found()?, this));
560559
};
@@ -597,14 +596,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
597596

598597
fn write(
599598
&mut self,
600-
fd: i32,
599+
fd_num: i32,
601600
buf: Pointer,
602601
count: u64,
603602
offset: Option<i128>,
604603
) -> InterpResult<'tcx, Scalar> {
605604
let this = self.eval_context_mut();
606605

607-
// Isolation check is done via `FileDescriptor` trait.
606+
// Isolation check is done via `FileDescription` trait.
608607

609608
// Check that the *entire* buffer is actually valid memory.
610609
this.check_ptr_access(buf, Size::from_bytes(count), CheckInAllocMsg::MemoryAccessTest)?;
@@ -618,7 +617,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
618617

619618
let bytes = this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(count))?.to_owned();
620619
// We temporarily dup the FD to be able to retain mutable access to `this`.
621-
let Some(fd) = this.machine.fds.get(fd) else {
620+
let Some(fd) = this.machine.fds.get(fd_num) else {
622621
return Ok(Scalar::from_target_isize(this.fd_not_found()?, this));
623622
};
624623

0 commit comments

Comments
 (0)