Skip to content

Commit 29b0bbf

Browse files
committed
Auto merge of #1189 - RalfJung:cleanup, r=RalfJung
some foreign_items cleanup Cc @christianpoveda
2 parents 2752d1b + 4a9a0a9 commit 29b0bbf

File tree

6 files changed

+115
-110
lines changed

6 files changed

+115
-110
lines changed

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
169169
pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) -> Option<i64> {
170170
// FIXME: We always ignore leaks on some platforms where we do not
171171
// correctly implement TLS destructors.
172-
let target_os = tcx.sess.target.target.target_os.to_lowercase();
172+
let target_os = tcx.sess.target.target.target_os.as_str();
173173
let ignore_leaks = config.ignore_leaks || target_os == "windows" || target_os == "macos";
174174

175175
let (mut ecx, ret_place) = match create_ecx(tcx, main_id, config) {

src/helpers.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,25 +359,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
359359
/// Helper function used inside the shims of foreign functions to check that isolation is
360360
/// disabled. It returns an error using the `name` of the foreign function if this is not the
361361
/// case.
362-
fn check_no_isolation(&mut self, name: &str) -> InterpResult<'tcx> {
363-
if !self.eval_context_mut().machine.communicate {
362+
fn check_no_isolation(&self, name: &str) -> InterpResult<'tcx> {
363+
if !self.eval_context_ref().machine.communicate {
364364
throw_unsup_format!(
365365
"`{}` not available when isolation is enabled. Pass the flag `-Zmiri-disable-isolation` to disable it.",
366-
name
366+
name,
367367
)
368368
}
369369
Ok(())
370370
}
371371
/// Helper function used inside the shims of foreign functions to assert that the target
372372
/// platform is `platform`. It panics showing a message with the `name` of the foreign function
373373
/// if this is not the case.
374-
fn assert_platform(&mut self, platform: &str, name: &str) {
374+
fn assert_platform(&self, platform: &str, name: &str) {
375375
assert_eq!(
376-
self.eval_context_mut().tcx.sess.target.target.target_os.to_lowercase(),
376+
self.eval_context_ref().tcx.sess.target.target.target_os,
377377
platform,
378378
"`{}` is only available on the `{}` platform",
379379
name,
380-
platform
380+
platform,
381381
)
382382
}
383383

@@ -389,8 +389,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
389389
}
390390

391391
/// Gets the last error variable.
392-
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
393-
let this = self.eval_context_mut();
392+
fn get_last_error(&self) -> InterpResult<'tcx, Scalar<Tag>> {
393+
let this = self.eval_context_ref();
394394
let errno_place = this.machine.last_error.unwrap();
395395
this.read_scalar(errno_place.into())?.not_undef()
396396
}

src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
455455
this.write_scalar(Scalar::from_f64(res), dest)?;
456456
}
457457

458-
_ => match this.tcx.sess.target.target.target_os.to_lowercase().as_str() {
458+
_ => match this.tcx.sess.target.target.target_os.as_str() {
459459
"linux" | "macos" => return posix::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
460460
"windows" => return windows::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
461461
target => throw_unsup_format!("The {} target platform is not supported", target),

src/shims/foreign_items/posix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
322322
}
323323

324324
_ => {
325-
match this.tcx.sess.target.target.target_os.to_lowercase().as_str() {
325+
match this.tcx.sess.target.target.target_os.as_str() {
326326
"linux" => return linux::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
327327
"macos" => return macos::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
328328
_ => unreachable!(),

src/shims/foreign_items/posix/linux.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
1818
this.write_scalar(errno_place.to_ref().to_scalar()?, dest)?;
1919
}
2020

21-
// File related shims
21+
// File related shims (but also see "syscall" below for statx)
2222

2323
// The only reason this is not in the `posix` module is because the `macos` item has a
2424
// different name.
@@ -59,8 +59,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5959
// so skip over it.
6060
getrandom(this, &args[1..], dest)?;
6161
}
62-
// `statx` is used by `libstd` to retrieve metadata information in `linux`
63-
// instead of using `stat`,`lstat` or `fstat` as in the `macos` platform.
62+
// `statx` is used by `libstd` to retrieve metadata information on `linux`
63+
// instead of using `stat`,`lstat` or `fstat` as on `macos`.
6464
id if id == sys_statx => {
6565
// The first argument is the syscall id,
6666
// so skip over it.
@@ -87,7 +87,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8787
}
8888
}
8989

90-
// Shims the linux 'getrandom()' syscall.
90+
// Shims the linux `getrandom` syscall.
9191
fn getrandom<'tcx>(
9292
this: &mut MiriEvalContext<'_, 'tcx>,
9393
args: &[OpTy<'tcx, Tag>],

src/shims/fs.rs

Lines changed: 100 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,105 @@ impl FileHandler {
6363
}
6464
}
6565

66+
impl<'mir, 'tcx> EvalContextExtPrivate<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
67+
trait EvalContextExtPrivate<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
68+
/// Emulate `stat` or `lstat` on the `macos` platform. This function is not intended to be
69+
/// called directly from `emulate_foreign_item_by_name`, so it does not check if isolation is
70+
/// disabled or if the target platform is the correct one. Please use `macos_stat` or
71+
/// `macos_lstat` instead.
72+
fn macos_stat_or_lstat(
73+
&mut self,
74+
follow_symlink: bool,
75+
path_op: OpTy<'tcx, Tag>,
76+
buf_op: OpTy<'tcx, Tag>,
77+
) -> InterpResult<'tcx, i32> {
78+
let this = self.eval_context_mut();
79+
80+
let path_scalar = this.read_scalar(path_op)?.not_undef()?;
81+
let path: PathBuf = this.read_os_str_from_c_str(path_scalar)?.into();
82+
83+
let metadata = match FileMetadata::from_path(this, path, follow_symlink)? {
84+
Some(metadata) => metadata,
85+
None => return Ok(-1),
86+
};
87+
this.macos_stat_write_buf(metadata, buf_op)
88+
}
89+
90+
fn macos_stat_write_buf(
91+
&mut self,
92+
metadata: FileMetadata,
93+
buf_op: OpTy<'tcx, Tag>,
94+
) -> InterpResult<'tcx, i32> {
95+
let this = self.eval_context_mut();
96+
97+
let mode: u16 = metadata.mode.to_u16()?;
98+
99+
let (access_sec, access_nsec) = metadata.accessed.unwrap_or((0, 0));
100+
let (created_sec, created_nsec) = metadata.created.unwrap_or((0, 0));
101+
let (modified_sec, modified_nsec) = metadata.modified.unwrap_or((0, 0));
102+
103+
let dev_t_layout = this.libc_ty_layout("dev_t")?;
104+
let mode_t_layout = this.libc_ty_layout("mode_t")?;
105+
let nlink_t_layout = this.libc_ty_layout("nlink_t")?;
106+
let ino_t_layout = this.libc_ty_layout("ino_t")?;
107+
let uid_t_layout = this.libc_ty_layout("uid_t")?;
108+
let gid_t_layout = this.libc_ty_layout("gid_t")?;
109+
let time_t_layout = this.libc_ty_layout("time_t")?;
110+
let long_layout = this.libc_ty_layout("c_long")?;
111+
let off_t_layout = this.libc_ty_layout("off_t")?;
112+
let blkcnt_t_layout = this.libc_ty_layout("blkcnt_t")?;
113+
let blksize_t_layout = this.libc_ty_layout("blksize_t")?;
114+
let uint32_t_layout = this.libc_ty_layout("uint32_t")?;
115+
116+
// We need to add 32 bits of padding after `st_rdev` if we are on a 64-bit platform.
117+
let pad_layout = if this.tcx.sess.target.ptr_width == 64 {
118+
uint32_t_layout
119+
} else {
120+
this.layout_of(this.tcx.mk_unit())?
121+
};
122+
123+
let imms = [
124+
immty_from_uint_checked(0u128, dev_t_layout)?, // st_dev
125+
immty_from_uint_checked(mode, mode_t_layout)?, // st_mode
126+
immty_from_uint_checked(0u128, nlink_t_layout)?, // st_nlink
127+
immty_from_uint_checked(0u128, ino_t_layout)?, // st_ino
128+
immty_from_uint_checked(0u128, uid_t_layout)?, // st_uid
129+
immty_from_uint_checked(0u128, gid_t_layout)?, // st_gid
130+
immty_from_uint_checked(0u128, dev_t_layout)?, // st_rdev
131+
immty_from_uint_checked(0u128, pad_layout)?, // padding for 64-bit targets
132+
immty_from_uint_checked(access_sec, time_t_layout)?, // st_atime
133+
immty_from_uint_checked(access_nsec, long_layout)?, // st_atime_nsec
134+
immty_from_uint_checked(modified_sec, time_t_layout)?, // st_mtime
135+
immty_from_uint_checked(modified_nsec, long_layout)?, // st_mtime_nsec
136+
immty_from_uint_checked(0u128, time_t_layout)?, // st_ctime
137+
immty_from_uint_checked(0u128, long_layout)?, // st_ctime_nsec
138+
immty_from_uint_checked(created_sec, time_t_layout)?, // st_birthtime
139+
immty_from_uint_checked(created_nsec, long_layout)?, // st_birthtime_nsec
140+
immty_from_uint_checked(metadata.size, off_t_layout)?, // st_size
141+
immty_from_uint_checked(0u128, blkcnt_t_layout)?, // st_blocks
142+
immty_from_uint_checked(0u128, blksize_t_layout)?, // st_blksize
143+
immty_from_uint_checked(0u128, uint32_t_layout)?, // st_flags
144+
immty_from_uint_checked(0u128, uint32_t_layout)?, // st_gen
145+
];
146+
147+
let buf = this.deref_operand(buf_op)?;
148+
this.write_packed_immediates(buf, &imms)?;
149+
150+
Ok(0)
151+
}
152+
153+
/// Function used when a handle is not found inside `FileHandler`. It returns `Ok(-1)`and sets
154+
/// the last OS error to `libc::EBADF` (invalid file descriptor). This function uses
155+
/// `T: From<i32>` instead of `i32` directly because some fs functions return different integer
156+
/// types (like `read`, that returns an `i64`).
157+
fn handle_not_found<T: From<i32>>(&mut self) -> InterpResult<'tcx, T> {
158+
let this = self.eval_context_mut();
159+
let ebadf = this.eval_libc("EBADF")?;
160+
this.set_last_error(ebadf)?;
161+
Ok((-1).into())
162+
}
163+
}
164+
66165
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
67166
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
68167
fn open(
@@ -432,29 +531,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
432531
Some(metadata) => metadata,
433532
None => return Ok(-1),
434533
};
435-
macos_stat_write_buf(this, metadata, buf_op)
436-
}
437-
438-
/// Emulate `stat` or `lstat` on the `macos` platform. This function is not intended to be
439-
/// called directly from `emulate_foreign_item_by_name`, so it does not check if isolation is
440-
/// disabled or if the target platform is the correct one. Please use `macos_stat` or
441-
/// `macos_lstat` instead.
442-
fn macos_stat_or_lstat(
443-
&mut self,
444-
follow_symlink: bool,
445-
path_op: OpTy<'tcx, Tag>,
446-
buf_op: OpTy<'tcx, Tag>,
447-
) -> InterpResult<'tcx, i32> {
448-
let this = self.eval_context_mut();
449-
450-
let path_scalar = this.read_scalar(path_op)?.not_undef()?;
451-
let path: PathBuf = this.read_os_str_from_c_str(path_scalar)?.into();
452-
453-
let metadata = match FileMetadata::from_path(this, path, follow_symlink)? {
454-
Some(metadata) => metadata,
455-
None => return Ok(-1),
456-
};
457-
macos_stat_write_buf(this, metadata, buf_op)
534+
this.macos_stat_write_buf(metadata, buf_op)
458535
}
459536

460537
fn linux_statx(
@@ -620,17 +697,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
620697
Ok(0)
621698
}
622699

623-
/// Function used when a handle is not found inside `FileHandler`. It returns `Ok(-1)`and sets
624-
/// the last OS error to `libc::EBADF` (invalid file descriptor). This function uses
625-
/// `T: From<i32>` instead of `i32` directly because some fs functions return different integer
626-
/// types (like `read`, that returns an `i64`).
627-
fn handle_not_found<T: From<i32>>(&mut self) -> InterpResult<'tcx, T> {
628-
let this = self.eval_context_mut();
629-
let ebadf = this.eval_libc("EBADF")?;
630-
this.set_last_error(ebadf)?;
631-
Ok((-1).into())
632-
}
633-
634700
fn rename(
635701
&mut self,
636702
oldpath_op: OpTy<'tcx, Tag>,
@@ -743,64 +809,3 @@ impl FileMetadata {
743809
Ok(Some(FileMetadata { mode, size, created, accessed, modified }))
744810
}
745811
}
746-
747-
fn macos_stat_write_buf<'tcx, 'mir>(
748-
ecx: &mut MiriEvalContext<'mir, 'tcx>,
749-
metadata: FileMetadata,
750-
buf_op: OpTy<'tcx, Tag>,
751-
) -> InterpResult<'tcx, i32> {
752-
let mode: u16 = metadata.mode.to_u16()?;
753-
754-
let (access_sec, access_nsec) = metadata.accessed.unwrap_or((0, 0));
755-
let (created_sec, created_nsec) = metadata.created.unwrap_or((0, 0));
756-
let (modified_sec, modified_nsec) = metadata.modified.unwrap_or((0, 0));
757-
758-
let dev_t_layout = ecx.libc_ty_layout("dev_t")?;
759-
let mode_t_layout = ecx.libc_ty_layout("mode_t")?;
760-
let nlink_t_layout = ecx.libc_ty_layout("nlink_t")?;
761-
let ino_t_layout = ecx.libc_ty_layout("ino_t")?;
762-
let uid_t_layout = ecx.libc_ty_layout("uid_t")?;
763-
let gid_t_layout = ecx.libc_ty_layout("gid_t")?;
764-
let time_t_layout = ecx.libc_ty_layout("time_t")?;
765-
let long_layout = ecx.libc_ty_layout("c_long")?;
766-
let off_t_layout = ecx.libc_ty_layout("off_t")?;
767-
let blkcnt_t_layout = ecx.libc_ty_layout("blkcnt_t")?;
768-
let blksize_t_layout = ecx.libc_ty_layout("blksize_t")?;
769-
let uint32_t_layout = ecx.libc_ty_layout("uint32_t")?;
770-
771-
// We need to add 32 bits of padding after `st_rdev` if we are on a 64-bit platform.
772-
let pad_layout = if ecx.tcx.sess.target.ptr_width == 64 {
773-
uint32_t_layout
774-
} else {
775-
ecx.layout_of(ecx.tcx.mk_unit())?
776-
};
777-
778-
let imms = [
779-
immty_from_uint_checked(0u128, dev_t_layout)?, // st_dev
780-
immty_from_uint_checked(mode, mode_t_layout)?, // st_mode
781-
immty_from_uint_checked(0u128, nlink_t_layout)?, // st_nlink
782-
immty_from_uint_checked(0u128, ino_t_layout)?, // st_ino
783-
immty_from_uint_checked(0u128, uid_t_layout)?, // st_uid
784-
immty_from_uint_checked(0u128, gid_t_layout)?, // st_gid
785-
immty_from_uint_checked(0u128, dev_t_layout)?, // st_rdev
786-
immty_from_uint_checked(0u128, pad_layout)?, // padding for 64-bit targets
787-
immty_from_uint_checked(access_sec, time_t_layout)?, // st_atime
788-
immty_from_uint_checked(access_nsec, long_layout)?, // st_atime_nsec
789-
immty_from_uint_checked(modified_sec, time_t_layout)?, // st_mtime
790-
immty_from_uint_checked(modified_nsec, long_layout)?, // st_mtime_nsec
791-
immty_from_uint_checked(0u128, time_t_layout)?, // st_ctime
792-
immty_from_uint_checked(0u128, long_layout)?, // st_ctime_nsec
793-
immty_from_uint_checked(created_sec, time_t_layout)?, // st_birthtime
794-
immty_from_uint_checked(created_nsec, long_layout)?, // st_birthtime_nsec
795-
immty_from_uint_checked(metadata.size, off_t_layout)?, // st_size
796-
immty_from_uint_checked(0u128, blkcnt_t_layout)?, // st_blocks
797-
immty_from_uint_checked(0u128, blksize_t_layout)?, // st_blksize
798-
immty_from_uint_checked(0u128, uint32_t_layout)?, // st_flags
799-
immty_from_uint_checked(0u128, uint32_t_layout)?, // st_gen
800-
];
801-
802-
let buf = ecx.deref_operand(buf_op)?;
803-
ecx.write_packed_immediates(buf, &imms)?;
804-
805-
Ok(0)
806-
}

0 commit comments

Comments
 (0)