2020-03-17 09:18:53 -05:00
|
|
|
use std::convert::TryFrom;
|
2021-05-16 04:28:01 -05:00
|
|
|
use std::env;
|
|
|
|
use std::ffi::{OsStr, OsString};
|
2021-06-09 08:28:35 -05:00
|
|
|
use std::io::ErrorKind;
|
2019-08-06 17:40:07 -05:00
|
|
|
|
2020-03-02 15:36:15 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2021-09-06 10:05:48 -05:00
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
2019-12-27 19:32:20 -06:00
|
|
|
use rustc_mir::interpret::Pointer;
|
2021-09-06 10:05:48 -05:00
|
|
|
use rustc_target::abi::Size;
|
2019-08-06 17:40:07 -05:00
|
|
|
|
2020-04-02 17:05:35 -05:00
|
|
|
use crate::*;
|
|
|
|
|
2020-03-29 12:13:42 -05:00
|
|
|
/// Check whether an operation that writes to a target buffer was successful.
|
|
|
|
/// Accordingly select return value.
|
|
|
|
/// Local helper function to be used in Windows shims.
|
|
|
|
fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
|
|
|
|
if success {
|
|
|
|
// If the function succeeds, the return value is the number of characters stored in the target buffer,
|
|
|
|
// not including the terminating null character.
|
|
|
|
u32::try_from(len).unwrap()
|
|
|
|
} else {
|
|
|
|
// If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
|
|
|
|
// required to hold the string and its terminating null character.
|
|
|
|
u32::try_from(len.checked_add(1).unwrap()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:34:43 -05:00
|
|
|
#[derive(Default)]
|
2020-03-08 11:54:47 -05:00
|
|
|
pub struct EnvVars<'tcx> {
|
2019-08-27 08:45:37 -05:00
|
|
|
/// Stores pointers to the environment variables. These variables must be stored as
|
2020-03-23 14:08:57 -05:00
|
|
|
/// null-terminated target strings (c_str or wide_str) with the `"{name}={value}"` format.
|
2021-07-15 13:33:08 -05:00
|
|
|
map: FxHashMap<OsString, Pointer<Option<Tag>>>,
|
2020-03-08 11:54:47 -05:00
|
|
|
|
|
|
|
/// Place where the `environ` static is stored. Lazily initialized, but then never changes.
|
|
|
|
pub(crate) environ: Option<MPlaceTy<'tcx, Tag>>,
|
2019-08-13 11:34:43 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 11:54:47 -05:00
|
|
|
impl<'tcx> EnvVars<'tcx> {
|
|
|
|
pub(crate) fn init<'mir>(
|
2020-04-01 18:55:52 -05:00
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
|
2020-03-24 15:00:11 -05:00
|
|
|
mut excluded_env_vars: Vec<String>,
|
2020-03-07 10:35:00 -06:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-11-11 03:29:10 -06:00
|
|
|
let target_os = ecx.tcx.sess.target.os.as_str();
|
2020-03-27 08:18:13 -05:00
|
|
|
if target_os == "windows" {
|
2020-03-27 08:59:42 -05:00
|
|
|
// Temporary hack: Exclude `TERM` var to avoid terminfo trying to open the termcap file.
|
2020-03-27 09:15:35 -05:00
|
|
|
// Can be removed once https://github.com/rust-lang/miri/issues/1013 is resolved.
|
2020-03-24 15:00:11 -05:00
|
|
|
excluded_env_vars.push("TERM".to_owned());
|
|
|
|
}
|
2020-03-27 08:59:42 -05:00
|
|
|
|
2021-05-14 01:40:07 -05:00
|
|
|
if ecx.machine.communicate() {
|
2019-09-18 16:10:13 -05:00
|
|
|
for (name, value) in env::vars() {
|
2019-08-28 17:31:57 -05:00
|
|
|
if !excluded_env_vars.contains(&name) {
|
2020-03-27 06:07:21 -05:00
|
|
|
let var_ptr = match target_os {
|
2021-05-16 04:28:01 -05:00
|
|
|
"linux" | "macos" =>
|
|
|
|
alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx)?,
|
2020-03-27 06:07:21 -05:00
|
|
|
"windows" => alloc_env_var_as_wide_str(name.as_ref(), value.as_ref(), ecx)?,
|
2021-07-11 07:18:44 -05:00
|
|
|
unsupported =>
|
|
|
|
throw_unsup_format!(
|
|
|
|
"environment support for target OS `{}` not yet available",
|
|
|
|
unsupported
|
|
|
|
),
|
2020-03-27 06:07:21 -05:00
|
|
|
};
|
2019-12-27 19:32:20 -06:00
|
|
|
ecx.machine.env_vars.map.insert(OsString::from(name), var_ptr);
|
2019-08-28 17:31:57 -05:00
|
|
|
}
|
2019-08-13 16:17:41 -05:00
|
|
|
}
|
2019-08-13 11:34:43 -05:00
|
|
|
}
|
2020-03-07 10:35:00 -06:00
|
|
|
ecx.update_environ()
|
2019-08-13 11:34:43 -05:00
|
|
|
}
|
2020-03-28 05:06:56 -05:00
|
|
|
|
|
|
|
pub(crate) fn cleanup<'mir>(
|
2020-04-01 18:55:52 -05:00
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
|
2020-03-28 05:06:56 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
// Deallocate individual env vars.
|
|
|
|
for (_name, ptr) in ecx.machine.env_vars.map.drain() {
|
|
|
|
ecx.memory.deallocate(ptr, None, MiriMemoryKind::Env.into())?;
|
|
|
|
}
|
|
|
|
// Deallocate environ var list.
|
2020-03-28 08:32:50 -05:00
|
|
|
let environ = ecx.machine.env_vars.environ.unwrap();
|
2021-07-15 13:33:08 -05:00
|
|
|
let old_vars_ptr = ecx.read_pointer(&environ.into())?;
|
|
|
|
ecx.memory.deallocate(old_vars_ptr, None, MiriMemoryKind::Env.into())?;
|
2020-03-28 05:06:56 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-08-13 11:34:43 -05:00
|
|
|
}
|
|
|
|
|
2020-03-26 23:10:45 -05:00
|
|
|
fn alloc_env_var_as_c_str<'mir, 'tcx>(
|
|
|
|
name: &OsStr,
|
|
|
|
value: &OsStr,
|
2020-04-01 18:55:52 -05:00
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
|
2021-07-15 13:33:08 -05:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
|
2020-03-26 23:10:45 -05:00
|
|
|
let mut name_osstring = name.to_os_string();
|
|
|
|
name_osstring.push("=");
|
|
|
|
name_osstring.push(value);
|
2021-07-04 08:59:55 -05:00
|
|
|
ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Env.into())
|
2020-03-26 23:10:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn alloc_env_var_as_wide_str<'mir, 'tcx>(
|
|
|
|
name: &OsStr,
|
|
|
|
value: &OsStr,
|
2020-04-01 18:55:52 -05:00
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
|
2021-07-15 13:33:08 -05:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
|
2020-03-26 23:10:45 -05:00
|
|
|
let mut name_osstring = name.to_os_string();
|
|
|
|
name_osstring.push("=");
|
|
|
|
name_osstring.push(value);
|
2021-07-04 08:59:55 -05:00
|
|
|
ecx.alloc_os_str_as_wide_str(name_osstring.as_os_str(), MiriMemoryKind::Env.into())
|
2020-03-26 23:10:45 -05:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:55:52 -05:00
|
|
|
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
2019-08-14 15:44:37 -05:00
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2021-07-15 13:33:08 -05:00
|
|
|
fn getenv(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
|
2019-08-14 15:44:37 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-11-11 03:29:10 -06:00
|
|
|
let target_os = &this.tcx.sess.target.os;
|
2021-05-16 04:28:01 -05:00
|
|
|
assert!(
|
|
|
|
target_os == "linux" || target_os == "macos",
|
|
|
|
"`getenv` is only available for the UNIX target family"
|
|
|
|
);
|
2019-08-14 15:44:37 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let name_ptr = this.read_pointer(name_op)?;
|
2019-12-27 19:32:20 -06:00
|
|
|
let name = this.read_os_str_from_c_str(name_ptr)?;
|
2019-08-14 16:48:36 -05:00
|
|
|
Ok(match this.machine.env_vars.map.get(name) {
|
2019-09-20 10:25:43 -05:00
|
|
|
Some(var_ptr) => {
|
2020-03-26 23:10:45 -05:00
|
|
|
// The offset is used to strip the "{name}=" part of the string.
|
2021-07-15 13:33:08 -05:00
|
|
|
var_ptr.offset(
|
2021-05-16 04:28:01 -05:00
|
|
|
Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()),
|
|
|
|
this,
|
2021-07-15 13:33:08 -05:00
|
|
|
)?
|
2019-09-20 10:25:43 -05:00
|
|
|
}
|
2021-07-15 13:33:08 -05:00
|
|
|
None => Pointer::null(),
|
2019-08-14 16:48:36 -05:00
|
|
|
})
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|
|
|
|
|
2020-03-25 09:38:27 -05:00
|
|
|
#[allow(non_snake_case)]
|
2020-03-24 23:52:53 -05:00
|
|
|
fn GetEnvironmentVariableW(
|
2020-03-13 11:45:02 -05:00
|
|
|
&mut self,
|
2021-05-16 04:28:01 -05:00
|
|
|
name_op: &OpTy<'tcx, Tag>, // LPCWSTR
|
|
|
|
buf_op: &OpTy<'tcx, Tag>, // LPWSTR
|
|
|
|
size_op: &OpTy<'tcx, Tag>, // DWORD
|
|
|
|
) -> InterpResult<'tcx, u32> {
|
|
|
|
// ^ Returns DWORD (u32 on Windows)
|
|
|
|
|
2020-03-13 11:45:02 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.assert_target_os("windows", "GetEnvironmentVariableW");
|
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let name_ptr = this.read_pointer(name_op)?;
|
2020-03-13 11:45:02 -05:00
|
|
|
let name = this.read_os_str_from_wide_str(name_ptr)?;
|
|
|
|
Ok(match this.machine.env_vars.map.get(&name) {
|
|
|
|
Some(var_ptr) => {
|
|
|
|
// The offset is used to strip the "{name}=" part of the string.
|
2021-05-16 04:28:01 -05:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let name_offset_bytes = u64::try_from(name.len()).unwrap()
|
|
|
|
.checked_add(1).unwrap()
|
|
|
|
.checked_mul(2).unwrap();
|
2021-07-15 13:33:08 -05:00
|
|
|
let var_ptr = var_ptr.offset(Size::from_bytes(name_offset_bytes), this)?;
|
2020-03-27 08:18:13 -05:00
|
|
|
let var = this.read_os_str_from_wide_str(var_ptr)?;
|
2020-03-13 11:45:02 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let buf_ptr = this.read_pointer(buf_op)?;
|
2020-03-25 09:38:27 -05:00
|
|
|
// `buf_size` represents the size in characters.
|
2020-03-28 11:35:40 -05:00
|
|
|
let buf_size = u64::from(this.read_scalar(size_op)?.to_u32()?);
|
2020-03-29 12:10:23 -05:00
|
|
|
windows_check_buffer_size(this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?)
|
2020-03-13 11:45:02 -05:00
|
|
|
}
|
|
|
|
None => {
|
2020-03-31 10:26:32 -05:00
|
|
|
let envvar_not_found = this.eval_windows("c", "ERROR_ENVVAR_NOT_FOUND")?;
|
2020-03-28 13:44:41 -05:00
|
|
|
this.set_last_error(envvar_not_found)?;
|
2020-03-13 11:45:02 -05:00
|
|
|
0 // return zero upon failure
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:38:27 -05:00
|
|
|
#[allow(non_snake_case)]
|
2021-07-15 13:33:08 -05:00
|
|
|
fn GetEnvironmentStringsW(&mut self) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
|
2020-03-13 11:45:02 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.assert_target_os("windows", "GetEnvironmentStringsW");
|
|
|
|
|
2020-07-23 10:50:45 -05:00
|
|
|
// Info on layout of environment blocks in Windows:
|
2020-03-13 11:45:02 -05:00
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables
|
|
|
|
let mut env_vars = std::ffi::OsString::new();
|
2020-03-25 09:38:27 -05:00
|
|
|
for &item in this.machine.env_vars.map.values() {
|
2021-07-15 13:33:08 -05:00
|
|
|
let env_var = this.read_os_str_from_wide_str(item)?;
|
2020-03-13 11:45:02 -05:00
|
|
|
env_vars.push(env_var);
|
|
|
|
env_vars.push("\0");
|
|
|
|
}
|
2020-03-24 23:52:53 -05:00
|
|
|
// Allocate environment block & Store environment variables to environment block.
|
|
|
|
// Final null terminator(block terminator) is added by `alloc_os_str_to_wide_str`.
|
2021-07-04 08:59:55 -05:00
|
|
|
let envblock_ptr = this.alloc_os_str_as_wide_str(&env_vars, MiriMemoryKind::Env.into())?;
|
2020-03-25 09:38:27 -05:00
|
|
|
// If the function succeeds, the return value is a pointer to the environment block of the current process.
|
2021-07-15 13:33:08 -05:00
|
|
|
Ok(envblock_ptr)
|
2020-03-24 23:52:53 -05:00
|
|
|
}
|
|
|
|
|
2020-03-25 09:38:27 -05:00
|
|
|
#[allow(non_snake_case)]
|
2021-05-16 04:28:01 -05:00
|
|
|
fn FreeEnvironmentStringsW(
|
|
|
|
&mut self,
|
|
|
|
env_block_op: &OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
2020-03-24 23:52:53 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.assert_target_os("windows", "FreeEnvironmentStringsW");
|
2020-03-13 11:45:02 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let env_block_ptr = this.read_pointer(env_block_op)?;
|
|
|
|
let result = this.memory.deallocate(env_block_ptr, None, MiriMemoryKind::Env.into());
|
2020-03-25 09:38:27 -05:00
|
|
|
// If the function succeeds, the return value is nonzero.
|
|
|
|
Ok(result.is_ok() as i32)
|
2020-03-13 11:45:02 -05:00
|
|
|
}
|
|
|
|
|
2019-08-14 15:44:37 -05:00
|
|
|
fn setenv(
|
|
|
|
&mut self,
|
2021-02-19 18:00:00 -06:00
|
|
|
name_op: &OpTy<'tcx, Tag>,
|
|
|
|
value_op: &OpTy<'tcx, Tag>,
|
2019-08-14 16:48:36 -05:00
|
|
|
) -> InterpResult<'tcx, i32> {
|
2019-12-27 19:32:20 -06:00
|
|
|
let mut this = self.eval_context_mut();
|
2020-11-11 03:29:10 -06:00
|
|
|
let target_os = &this.tcx.sess.target.os;
|
2021-05-16 04:28:01 -05:00
|
|
|
assert!(
|
|
|
|
target_os == "linux" || target_os == "macos",
|
|
|
|
"`setenv` is only available for the UNIX target family"
|
|
|
|
);
|
2019-08-14 15:44:37 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let name_ptr = this.read_pointer(name_op)?;
|
|
|
|
let value_ptr = this.read_pointer(value_op)?;
|
2020-03-26 23:10:45 -05:00
|
|
|
|
2019-08-14 16:48:36 -05:00
|
|
|
let mut new = None;
|
2021-07-15 13:33:08 -05:00
|
|
|
if !this.ptr_is_null(name_ptr)? {
|
2020-03-26 23:10:45 -05:00
|
|
|
let name = this.read_os_str_from_c_str(name_ptr)?;
|
2019-12-27 19:32:20 -06:00
|
|
|
if !name.is_empty() && !name.to_string_lossy().contains('=') {
|
2020-03-26 23:10:45 -05:00
|
|
|
let value = this.read_os_str_from_c_str(value_ptr)?;
|
2019-08-14 15:44:37 -05:00
|
|
|
new = Some((name.to_owned(), value.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some((name, value)) = new {
|
2020-03-26 23:10:45 -05:00
|
|
|
let var_ptr = alloc_env_var_as_c_str(&name, &value, &mut this)?;
|
2020-03-23 14:08:57 -05:00
|
|
|
if let Some(var) = this.machine.env_vars.map.insert(name, var_ptr) {
|
2021-05-16 04:28:01 -05:00
|
|
|
this.memory.deallocate(var, None, MiriMemoryKind::Env.into())?;
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|
2020-03-05 16:25:55 -06:00
|
|
|
this.update_environ()?;
|
2020-03-26 23:10:45 -05:00
|
|
|
Ok(0) // return zero on success
|
2019-08-14 15:44:37 -05:00
|
|
|
} else {
|
2020-03-26 23:10:45 -05:00
|
|
|
// name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
|
|
|
|
let einval = this.eval_libc("EINVAL")?;
|
|
|
|
this.set_last_error(einval)?;
|
2019-08-14 16:48:36 -05:00
|
|
|
Ok(-1)
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:38:27 -05:00
|
|
|
#[allow(non_snake_case)]
|
2020-03-24 23:52:53 -05:00
|
|
|
fn SetEnvironmentVariableW(
|
2020-03-13 11:45:02 -05:00
|
|
|
&mut self,
|
2021-02-19 18:00:00 -06:00
|
|
|
name_op: &OpTy<'tcx, Tag>, // LPCWSTR
|
|
|
|
value_op: &OpTy<'tcx, Tag>, // LPCWSTR
|
2020-03-13 11:45:02 -05:00
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let mut this = self.eval_context_mut();
|
|
|
|
this.assert_target_os("windows", "SetEnvironmentVariableW");
|
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let name_ptr = this.read_pointer(name_op)?;
|
|
|
|
let value_ptr = this.read_pointer(value_op)?;
|
2020-03-13 11:45:02 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
if this.ptr_is_null(name_ptr)? {
|
2020-03-24 23:52:53 -05:00
|
|
|
// ERROR CODE is not clearly explained in docs.. For now, throw UB instead.
|
2020-03-25 09:38:27 -05:00
|
|
|
throw_ub_format!("pointer to environment variable name is NULL");
|
2020-03-13 11:45:02 -05:00
|
|
|
}
|
2020-07-23 10:50:45 -05:00
|
|
|
|
2020-03-24 23:52:53 -05:00
|
|
|
let name = this.read_os_str_from_wide_str(name_ptr)?;
|
|
|
|
if name.is_empty() {
|
2020-03-25 09:38:27 -05:00
|
|
|
throw_unsup_format!("environment variable name is an empty string");
|
2020-03-24 23:52:53 -05:00
|
|
|
} else if name.to_string_lossy().contains('=') {
|
2020-03-25 09:38:27 -05:00
|
|
|
throw_unsup_format!("environment variable name contains '='");
|
2021-07-15 13:33:08 -05:00
|
|
|
} else if this.ptr_is_null(value_ptr)? {
|
2020-03-24 23:52:53 -05:00
|
|
|
// Delete environment variable `{name}`
|
|
|
|
if let Some(var) = this.machine.env_vars.map.remove(&name) {
|
2020-03-28 05:06:56 -05:00
|
|
|
this.memory.deallocate(var, None, MiriMemoryKind::Env.into())?;
|
2020-03-24 23:52:53 -05:00
|
|
|
this.update_environ()?;
|
|
|
|
}
|
|
|
|
Ok(1) // return non-zero on success
|
|
|
|
} else {
|
|
|
|
let value = this.read_os_str_from_wide_str(value_ptr)?;
|
|
|
|
let var_ptr = alloc_env_var_as_wide_str(&name, &value, &mut this)?;
|
2020-03-13 11:45:02 -05:00
|
|
|
if let Some(var) = this.machine.env_vars.map.insert(name, var_ptr) {
|
2021-05-16 04:28:01 -05:00
|
|
|
this.memory.deallocate(var, None, MiriMemoryKind::Env.into())?;
|
2020-03-13 11:45:02 -05:00
|
|
|
}
|
|
|
|
this.update_environ()?;
|
|
|
|
Ok(1) // return non-zero on success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 18:00:00 -06:00
|
|
|
fn unsetenv(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
2019-08-14 15:44:37 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-11-11 03:29:10 -06:00
|
|
|
let target_os = &this.tcx.sess.target.os;
|
2021-05-16 04:28:01 -05:00
|
|
|
assert!(
|
|
|
|
target_os == "linux" || target_os == "macos",
|
|
|
|
"`unsetenv` is only available for the UNIX target family"
|
|
|
|
);
|
2019-08-14 15:44:37 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let name_ptr = this.read_pointer(name_op)?;
|
2019-08-14 16:48:36 -05:00
|
|
|
let mut success = None;
|
2021-07-15 13:33:08 -05:00
|
|
|
if !this.ptr_is_null(name_ptr)? {
|
2020-03-24 23:52:53 -05:00
|
|
|
let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
|
2019-12-27 19:32:20 -06:00
|
|
|
if !name.is_empty() && !name.to_string_lossy().contains('=') {
|
2019-08-14 15:44:37 -05:00
|
|
|
success = Some(this.machine.env_vars.map.remove(&name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(old) = success {
|
|
|
|
if let Some(var) = old {
|
2021-05-16 04:28:01 -05:00
|
|
|
this.memory.deallocate(var, None, MiriMemoryKind::Env.into())?;
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|
2020-03-05 16:25:55 -06:00
|
|
|
this.update_environ()?;
|
2019-08-14 16:48:36 -05:00
|
|
|
Ok(0)
|
2019-08-14 15:44:37 -05:00
|
|
|
} else {
|
2020-03-26 23:10:45 -05:00
|
|
|
// name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
|
|
|
|
let einval = this.eval_libc("EINVAL")?;
|
|
|
|
this.set_last_error(einval)?;
|
2019-08-14 16:48:36 -05:00
|
|
|
Ok(-1)
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 16:10:13 -05:00
|
|
|
|
|
|
|
fn getcwd(
|
|
|
|
&mut self,
|
2021-02-19 18:00:00 -06:00
|
|
|
buf_op: &OpTy<'tcx, Tag>,
|
|
|
|
size_op: &OpTy<'tcx, Tag>,
|
2021-07-15 13:33:08 -05:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
|
2019-09-18 16:10:13 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-11-11 03:29:10 -06:00
|
|
|
let target_os = &this.tcx.sess.target.os;
|
2021-05-16 04:28:01 -05:00
|
|
|
assert!(
|
|
|
|
target_os == "linux" || target_os == "macos",
|
|
|
|
"`getcwd` is only available for the UNIX target family"
|
|
|
|
);
|
2019-09-18 16:10:13 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let buf = this.read_pointer(&buf_op)?;
|
|
|
|
let size = this.read_scalar(&size_op)?.to_machine_usize(&*this.tcx)?;
|
|
|
|
|
2021-05-14 01:40:07 -05:00
|
|
|
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
|
2021-06-15 19:33:18 -05:00
|
|
|
this.reject_in_isolation("`getcwd`", reject_with)?;
|
2021-06-09 11:21:23 -05:00
|
|
|
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;
|
2021-07-15 13:33:08 -05:00
|
|
|
return Ok(Pointer::null());
|
2021-05-14 01:40:07 -05:00
|
|
|
}
|
2019-09-19 10:32:18 -05:00
|
|
|
|
2019-09-20 02:13:48 -05:00
|
|
|
// If we cannot get the current directory, we return null
|
2019-09-20 03:30:55 -05:00
|
|
|
match env::current_dir() {
|
2019-09-20 10:25:43 -05:00
|
|
|
Ok(cwd) => {
|
2020-03-23 13:24:16 -05:00
|
|
|
if this.write_path_to_c_str(&cwd, buf, size)?.0 {
|
2019-10-22 03:13:11 -05:00
|
|
|
return Ok(buf);
|
2019-09-20 03:30:55 -05:00
|
|
|
}
|
2019-10-03 10:21:55 -05:00
|
|
|
let erange = this.eval_libc("ERANGE")?;
|
|
|
|
this.set_last_error(erange)?;
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2021-06-09 08:28:35 -05:00
|
|
|
Err(e) => this.set_last_error_from_io_error(e.kind())?,
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2021-05-14 01:40:07 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
Ok(Pointer::null())
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2019-09-24 14:42:38 -05:00
|
|
|
|
2020-03-28 13:44:41 -05:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn GetCurrentDirectoryW(
|
|
|
|
&mut self,
|
2021-02-19 18:00:00 -06:00
|
|
|
size_op: &OpTy<'tcx, Tag>, // DWORD
|
|
|
|
buf_op: &OpTy<'tcx, Tag>, // LPTSTR
|
2020-03-28 13:44:41 -05:00
|
|
|
) -> InterpResult<'tcx, u32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.assert_target_os("windows", "GetCurrentDirectoryW");
|
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let size = u64::from(this.read_scalar(size_op)?.to_u32()?);
|
|
|
|
let buf = this.read_pointer(buf_op)?;
|
|
|
|
|
2021-05-14 01:40:07 -05:00
|
|
|
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
|
2021-06-15 19:33:18 -05:00
|
|
|
this.reject_in_isolation("`GetCurrentDirectoryW`", reject_with)?;
|
2021-06-09 11:21:23 -05:00
|
|
|
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;
|
2021-05-14 01:40:07 -05:00
|
|
|
return Ok(0);
|
|
|
|
}
|
2020-03-28 13:44:41 -05:00
|
|
|
|
|
|
|
// If we cannot get the current directory, we return 0
|
|
|
|
match env::current_dir() {
|
2020-03-29 10:15:53 -05:00
|
|
|
Ok(cwd) =>
|
2020-03-29 12:10:23 -05:00
|
|
|
return Ok(windows_check_buffer_size(this.write_path_to_wide_str(&cwd, buf, size)?)),
|
2021-06-09 08:28:35 -05:00
|
|
|
Err(e) => this.set_last_error_from_io_error(e.kind())?,
|
2020-03-28 13:44:41 -05:00
|
|
|
}
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
2021-02-19 18:00:00 -06:00
|
|
|
fn chdir(&mut self, path_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
2019-09-24 14:42:38 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-11-11 03:29:10 -06:00
|
|
|
let target_os = &this.tcx.sess.target.os;
|
2021-05-16 04:28:01 -05:00
|
|
|
assert!(
|
|
|
|
target_os == "linux" || target_os == "macos",
|
|
|
|
"`getcwd` is only available for the UNIX target family"
|
|
|
|
);
|
2019-09-24 14:42:38 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let path = this.read_path_from_c_str(this.read_pointer(path_op)?)?;
|
|
|
|
|
2021-05-14 01:40:07 -05:00
|
|
|
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
|
2021-06-15 19:33:18 -05:00
|
|
|
this.reject_in_isolation("`chdir`", reject_with)?;
|
2021-06-09 11:21:23 -05:00
|
|
|
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;
|
2021-05-14 01:40:07 -05:00
|
|
|
|
|
|
|
return Ok(-1);
|
|
|
|
}
|
2019-09-24 14:42:38 -05:00
|
|
|
|
2019-10-17 10:21:06 -05:00
|
|
|
match env::set_current_dir(path) {
|
2019-09-24 14:42:38 -05:00
|
|
|
Ok(()) => Ok(0),
|
|
|
|
Err(e) => {
|
2021-06-09 08:28:35 -05:00
|
|
|
this.set_last_error_from_io_error(e.kind())?;
|
2019-09-24 14:42:38 -05:00
|
|
|
Ok(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 12:24:01 -06:00
|
|
|
|
2020-03-28 13:44:41 -05:00
|
|
|
#[allow(non_snake_case)]
|
2021-05-16 04:28:01 -05:00
|
|
|
fn SetCurrentDirectoryW(
|
2020-03-28 13:44:41 -05:00
|
|
|
&mut self,
|
2021-05-16 04:28:01 -05:00
|
|
|
path_op: &OpTy<'tcx, Tag>, // LPCTSTR
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
// ^ Returns BOOL (i32 on Windows)
|
|
|
|
|
2020-03-28 13:44:41 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.assert_target_os("windows", "SetCurrentDirectoryW");
|
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let path = this.read_path_from_wide_str(this.read_pointer(path_op)?)?;
|
|
|
|
|
2021-05-14 01:40:07 -05:00
|
|
|
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
|
2021-06-15 19:33:18 -05:00
|
|
|
this.reject_in_isolation("`SetCurrentDirectoryW`", reject_with)?;
|
2021-06-09 11:21:23 -05:00
|
|
|
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;
|
2021-05-14 01:40:07 -05:00
|
|
|
|
|
|
|
return Ok(0);
|
|
|
|
}
|
2020-03-28 13:44:41 -05:00
|
|
|
|
|
|
|
match env::set_current_dir(path) {
|
|
|
|
Ok(()) => Ok(1),
|
|
|
|
Err(e) => {
|
2021-06-09 08:28:35 -05:00
|
|
|
this.set_last_error_from_io_error(e.kind())?;
|
2020-03-28 13:44:41 -05:00
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 11:18:53 -05:00
|
|
|
/// Updates the `environ` static.
|
|
|
|
/// The first time it gets called, also initializes `extra.environ`.
|
2020-03-04 12:24:01 -06:00
|
|
|
fn update_environ(&mut self) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
2020-03-28 05:06:56 -05:00
|
|
|
// Deallocate the old environ list, if any.
|
2020-03-08 11:54:47 -05:00
|
|
|
if let Some(environ) = this.machine.env_vars.environ {
|
2021-07-15 13:33:08 -05:00
|
|
|
let old_vars_ptr = this.read_pointer(&environ.into())?;
|
|
|
|
this.memory.deallocate(old_vars_ptr, None, MiriMemoryKind::Env.into())?;
|
2020-03-08 11:18:53 -05:00
|
|
|
} else {
|
|
|
|
// No `environ` allocated yet, let's do that.
|
2020-07-27 05:53:39 -05:00
|
|
|
// This is memory backing an extern static, hence `ExternStatic`, not `Env`.
|
2020-04-18 10:53:54 -05:00
|
|
|
let layout = this.machine.layouts.usize;
|
2021-07-04 08:59:55 -05:00
|
|
|
let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into())?;
|
2020-03-08 11:54:47 -05:00
|
|
|
this.machine.env_vars.environ = Some(place);
|
2020-03-06 08:38:16 -06:00
|
|
|
}
|
2020-03-08 11:18:53 -05:00
|
|
|
|
2020-03-04 12:24:01 -06:00
|
|
|
// Collect all the pointers to each variable in a vector.
|
2021-07-15 13:33:08 -05:00
|
|
|
let mut vars: Vec<Pointer<Option<Tag>>> =
|
|
|
|
this.machine.env_vars.map.values().copied().collect();
|
2020-03-04 12:24:01 -06:00
|
|
|
// Add the trailing null pointer.
|
2021-07-15 13:33:08 -05:00
|
|
|
vars.push(Pointer::null());
|
2020-03-04 12:24:01 -06:00
|
|
|
// Make an array with all these pointers inside Miri.
|
|
|
|
let tcx = this.tcx;
|
|
|
|
let vars_layout =
|
2020-03-17 09:18:53 -05:00
|
|
|
this.layout_of(tcx.mk_array(tcx.types.usize, u64::try_from(vars.len()).unwrap()))?;
|
2021-07-04 08:59:55 -05:00
|
|
|
let vars_place = this.allocate(vars_layout, MiriMemoryKind::Env.into())?;
|
2020-03-04 12:24:01 -06:00
|
|
|
for (idx, var) in vars.into_iter().enumerate() {
|
2021-02-19 18:00:00 -06:00
|
|
|
let place = this.mplace_field(&vars_place, idx)?;
|
2021-07-15 13:33:08 -05:00
|
|
|
this.write_pointer(var, &place.into())?;
|
2020-03-04 12:24:01 -06:00
|
|
|
}
|
2021-07-15 13:33:08 -05:00
|
|
|
this.write_pointer(vars_place.ptr, &this.machine.env_vars.environ.unwrap().into())?;
|
2020-03-04 12:24:01 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|