Skip to content

Commit

Permalink
support home_dir on macOS, and merge some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 6, 2022
1 parent b533154 commit 208679c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 44 deletions.
36 changes: 36 additions & 0 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,42 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_int(super::UID, dest)?;
}

"getpwuid_r" if this.frame_in_std() => {
let [uid, pwd, buf, buflen, result] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
this.check_no_isolation("`getpwuid_r`")?;

let uid = this.read_scalar(uid)?.to_u32()?;
let pwd = this.deref_operand(pwd)?;
let buf = this.read_pointer(buf)?;
let buflen = this.read_scalar(buflen)?.to_machine_usize(this)?;
let result = this.deref_operand(result)?;

// Must be for "us".
if uid != crate::shims::unix::UID {
throw_unsup_format!("`getpwuid_r` on other users is not supported");
}

// Reset all fields to `uninit` to make sure nobody reads them.
// (This is a std-only shim so we are okay with such hacks.)
this.write_uninit(&pwd.into())?;

// We only set the home_dir field.
#[allow(deprecated)]
let home_dir = std::env::home_dir().unwrap();
let (written, _) = this.write_path_to_c_str(&home_dir, buf, buflen)?;
let pw_dir = this.mplace_field_named(&pwd, "pw_dir")?;
this.write_pointer(buf, &pw_dir.into())?;

if written {
this.write_pointer(pwd.ptr, &result.into())?;
this.write_null(dest)?;
} else {
this.write_null(&result.into())?;
this.write_scalar(this.eval_libc("ERANGE")?, dest)?;
}
}

// Platform-specific shims
_ => {
match this.tcx.sess.target.os.as_ref() {
Expand Down
35 changes: 0 additions & 35 deletions src/shims/unix/linux/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,41 +155,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_null(dest)?;
}

"getpwuid_r" if this.frame_in_std() => {
let [uid, pwd, buf, buflen, result] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
this.check_no_isolation("`getpwuid_r`")?;

let uid = this.read_scalar(uid)?.to_u32()?;
let pwd = this.deref_operand(pwd)?;
let buf = this.read_pointer(buf)?;
let buflen = this.read_scalar(buflen)?.to_machine_usize(this)?;
let result = this.deref_operand(result)?;

// Must be for "us".
if uid != crate::shims::unix::UID {
throw_unsup_format!("`getpwuid_r` on other users is not supported");
}

// Reset all fields to `uninit` to make sure nobody reads them.
this.write_uninit(&pwd.into())?;

// We only set the home_dir field.
#[allow(deprecated)]
let home_dir = std::env::home_dir().unwrap();
let (written, _) = this.write_path_to_c_str(&home_dir, buf, buflen)?;
let pw_dir = this.mplace_field_named(&pwd, "pw_dir")?;
this.write_pointer(buf, &pw_dir.into())?;

if written {
this.write_pointer(pwd.ptr, &result.into())?;
this.write_null(dest)?;
} else {
this.write_null(&result.into())?;
this.write_scalar(this.eval_libc("ERANGE")?, dest)?;
}
}

_ => return Ok(EmulateByNameResult::NotSupported),
};

Expand Down
5 changes: 5 additions & 0 deletions tests/pass/env/current_exe.rs → tests/pass/env/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
//@compile-flags: -Zmiri-disable-isolation
use std::env;

// Smoke-test various APIs in env::
fn main() {
// The actual value we get is a bit odd: we get the Miri binary that interprets us.
env::current_exe().unwrap();

env::remove_var("HOME"); // make sure we enter the interesting codepath
#[allow(deprecated)]
env::home_dir().unwrap();
}
9 changes: 0 additions & 9 deletions tests/pass/env/home.rs

This file was deleted.

0 comments on commit 208679c

Please sign in to comment.