Skip to content

Commit 79d147e

Browse files
committed
make home_dir work on macOS
1 parent 437d241 commit 79d147e

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

src/shims/unix/foreign_items.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,42 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
553553
this.write_int(super::UID, dest)?;
554554
}
555555

556+
"getpwuid_r" if this.frame_in_std() => {
557+
let [uid, pwd, buf, buflen, result] =
558+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
559+
this.check_no_isolation("`getpwuid_r`")?;
560+
561+
let uid = this.read_scalar(uid)?.to_u32()?;
562+
let pwd = this.deref_operand(pwd)?;
563+
let buf = this.read_pointer(buf)?;
564+
let buflen = this.read_scalar(buflen)?.to_machine_usize(this)?;
565+
let result = this.deref_operand(result)?;
566+
567+
// Must be for "us".
568+
if uid != crate::shims::unix::UID {
569+
throw_unsup_format!("`getpwuid_r` on other users is not supported");
570+
}
571+
572+
// Reset all fields to `uninit` to make sure nobody reads them.
573+
// (This is a std-only shim so we are okay with such hacks.)
574+
this.write_uninit(&pwd.into())?;
575+
576+
// We only set the home_dir field.
577+
#[allow(deprecated)]
578+
let home_dir = std::env::home_dir().unwrap();
579+
let (written, _) = this.write_path_to_c_str(&home_dir, buf, buflen)?;
580+
let pw_dir = this.mplace_field_named(&pwd, "pw_dir")?;
581+
this.write_pointer(buf, &pw_dir.into())?;
582+
583+
if written {
584+
this.write_pointer(pwd.ptr, &result.into())?;
585+
this.write_null(dest)?;
586+
} else {
587+
this.write_null(&result.into())?;
588+
this.write_scalar(this.eval_libc("ERANGE")?, dest)?;
589+
}
590+
}
591+
556592
// Platform-specific shims
557593
_ => {
558594
match this.tcx.sess.target.os.as_ref() {

src/shims/unix/linux/foreign_items.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -155,41 +155,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
155155
this.write_null(dest)?;
156156
}
157157

158-
"getpwuid_r" if this.frame_in_std() => {
159-
let [uid, pwd, buf, buflen, result] =
160-
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
161-
this.check_no_isolation("`getpwuid_r`")?;
162-
163-
let uid = this.read_scalar(uid)?.to_u32()?;
164-
let pwd = this.deref_operand(pwd)?;
165-
let buf = this.read_pointer(buf)?;
166-
let buflen = this.read_scalar(buflen)?.to_machine_usize(this)?;
167-
let result = this.deref_operand(result)?;
168-
169-
// Must be for "us".
170-
if uid != crate::shims::unix::UID {
171-
throw_unsup_format!("`getpwuid_r` on other users is not supported");
172-
}
173-
174-
// Reset all fields to `uninit` to make sure nobody reads them.
175-
this.write_uninit(&pwd.into())?;
176-
177-
// We only set the home_dir field.
178-
#[allow(deprecated)]
179-
let home_dir = std::env::home_dir().unwrap();
180-
let (written, _) = this.write_path_to_c_str(&home_dir, buf, buflen)?;
181-
let pw_dir = this.mplace_field_named(&pwd, "pw_dir")?;
182-
this.write_pointer(buf, &pw_dir.into())?;
183-
184-
if written {
185-
this.write_pointer(pwd.ptr, &result.into())?;
186-
this.write_null(dest)?;
187-
} else {
188-
this.write_null(&result.into())?;
189-
this.write_scalar(this.eval_libc("ERANGE")?, dest)?;
190-
}
191-
}
192-
193158
_ => return Ok(EmulateByNameResult::NotSupported),
194159
};
195160

tests/pass/env/home.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@only-target-linux: home_dir is only supported on Linux
1+
//@ignore-target-windows: home_dir is not supported on Windows
22
//@compile-flags: -Zmiri-disable-isolation
33
use std::env;
44

0 commit comments

Comments
 (0)