2019-12-27 19:32:20 -06:00
|
|
|
use std::ffi::{OsString, OsStr};
|
2019-09-18 16:10:13 -05:00
|
|
|
use std::env;
|
2019-08-06 17:40:07 -05:00
|
|
|
|
2019-08-13 11:34:43 -05:00
|
|
|
use crate::stacked_borrows::Tag;
|
2020-03-04 11:15:14 -06:00
|
|
|
use crate::rustc_target::abi::LayoutOf;
|
2019-08-06 17:40:07 -05:00
|
|
|
use crate::*;
|
2019-10-13 15:26:03 -05:00
|
|
|
|
2020-03-02 15:36:15 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-09-20 10:25:43 -05:00
|
|
|
use rustc::ty::layout::Size;
|
2019-12-27 19:32:20 -06:00
|
|
|
use rustc_mir::interpret::Pointer;
|
2019-08-06 17:40:07 -05:00
|
|
|
|
2019-08-13 11:34:43 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct EnvVars {
|
2019-08-27 08:45:37 -05:00
|
|
|
/// Stores pointers to the environment variables. These variables must be stored as
|
|
|
|
/// null-terminated C strings with the `"{name}={value}"` format.
|
2020-03-02 15:36:15 -06:00
|
|
|
map: FxHashMap<OsString, Pointer<Tag>>,
|
2019-08-13 11:34:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnvVars {
|
|
|
|
pub(crate) fn init<'mir, 'tcx>(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
|
2019-12-27 07:37:52 -06:00
|
|
|
excluded_env_vars: Vec<String>,
|
2020-03-07 10:35:00 -06:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-08-15 04:24:04 -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) {
|
2019-09-20 10:25:43 -05:00
|
|
|
let var_ptr =
|
2019-12-27 19:32:20 -06:00
|
|
|
alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 19:32:20 -06:00
|
|
|
fn alloc_env_var_as_c_str<'mir, 'tcx>(
|
|
|
|
name: &OsStr,
|
|
|
|
value: &OsStr,
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
|
2019-08-07 09:10:39 -05:00
|
|
|
) -> Pointer<Tag> {
|
2019-12-27 19:32:20 -06:00
|
|
|
let mut name_osstring = name.to_os_string();
|
|
|
|
name_osstring.push("=");
|
|
|
|
name_osstring.push(value);
|
2020-02-23 14:55:02 -06:00
|
|
|
ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())
|
2019-08-06 17:40:07 -05:00
|
|
|
}
|
2019-08-14 15:44:37 -05:00
|
|
|
|
|
|
|
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2019-09-20 10:25:43 -05:00
|
|
|
fn getenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>> {
|
2019-08-14 15:44:37 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-08-14 16:48:36 -05:00
|
|
|
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
|
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-08-27 08:45:37 -05:00
|
|
|
// The offset is used to strip the "{name}=" part of the string.
|
2019-09-20 10:25:43 -05:00
|
|
|
Some(var_ptr) => {
|
2019-11-29 04:17:44 -06:00
|
|
|
Scalar::from(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?)
|
2019-09-20 10:25:43 -05:00
|
|
|
}
|
2019-08-14 16:48:36 -05:00
|
|
|
None => Scalar::ptr_null(&*this.tcx),
|
|
|
|
})
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn setenv(
|
|
|
|
&mut self,
|
|
|
|
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();
|
2019-08-14 15:44:37 -05:00
|
|
|
|
|
|
|
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
|
|
|
|
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
|
2019-12-27 19:32:20 -06:00
|
|
|
let value = this.read_os_str_from_c_str(value_ptr)?;
|
2019-08-14 16:48:36 -05:00
|
|
|
let mut new = None;
|
2019-08-14 15:44:37 -05:00
|
|
|
if !this.is_null(name_ptr)? {
|
2019-12-27 19:32:20 -06:00
|
|
|
let name = this.read_os_str_from_c_str(name_ptr)?;
|
|
|
|
if !name.is_empty() && !name.to_string_lossy().contains('=') {
|
2019-08-14 15:44:37 -05:00
|
|
|
new = Some((name.to_owned(), value.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some((name, value)) = new {
|
2019-12-27 19:32:20 -06:00
|
|
|
let var_ptr = alloc_env_var_as_c_str(&name, &value, &mut this);
|
2019-08-26 15:18:11 -05:00
|
|
|
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
|
2019-10-17 21:11:50 -05:00
|
|
|
this.memory
|
2020-02-23 14:55:02 -06:00
|
|
|
.deallocate(var, None, MiriMemoryKind::Machine.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 {
|
2019-08-14 16:48:36 -05:00
|
|
|
Ok(-1)
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 10:25:43 -05: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();
|
|
|
|
|
|
|
|
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
|
2019-08-14 16:48:36 -05:00
|
|
|
let mut success = None;
|
2019-08-14 15:44:37 -05:00
|
|
|
if !this.is_null(name_ptr)? {
|
2019-12-27 19:32:20 -06:00
|
|
|
let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
|
|
|
|
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 {
|
2019-10-17 21:11:50 -05:00
|
|
|
this.memory
|
2020-02-23 14:55:02 -06:00
|
|
|
.deallocate(var, None, MiriMemoryKind::Machine.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 {
|
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,
|
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
size_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, Scalar<Tag>> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("getcwd")?;
|
2019-09-19 10:32:18 -05:00
|
|
|
|
2019-10-22 03:13:11 -05:00
|
|
|
let buf = this.read_scalar(buf_op)?.not_undef()?;
|
2019-11-08 15:07:52 -06:00
|
|
|
let size = this.read_scalar(size_op)?.to_machine_usize(&*this.tcx)?;
|
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-02-24 19:50:25 -06:00
|
|
|
if this.write_os_str_to_c_str(&OsString::from(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
|
|
|
}
|
2019-10-12 20:44:45 -05:00
|
|
|
Err(e) => this.set_last_error_from_io_error(e)?,
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2019-10-18 04:33:12 -05:00
|
|
|
Ok(Scalar::ptr_null(&*this.tcx))
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2019-09-24 14:42:38 -05:00
|
|
|
|
|
|
|
fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("chdir")?;
|
2019-09-24 14:42:38 -05:00
|
|
|
|
2019-12-04 03:43:36 -06:00
|
|
|
let path = this.read_os_str_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
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) => {
|
2019-10-12 20:44:45 -05:00
|
|
|
this.set_last_error_from_io_error(e)?;
|
2019-09-24 14:42:38 -05:00
|
|
|
Ok(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 12:24:01 -06:00
|
|
|
|
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-08 11:18:53 -05:00
|
|
|
// Deallocate the old environ value, if any.
|
|
|
|
if let Some(environ) = this.memory.extra.environ {
|
|
|
|
let old_vars_ptr = this.read_scalar(environ.into())?.not_undef()?;
|
2020-03-06 08:38:16 -06:00
|
|
|
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?;
|
2020-03-08 11:18:53 -05:00
|
|
|
} else {
|
|
|
|
// No `environ` allocated yet, let's do that.
|
|
|
|
let layout = this.layout_of(this.tcx.types.usize)?;
|
|
|
|
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
|
|
|
|
this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?;
|
|
|
|
this.memory.extra.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.
|
|
|
|
let mut vars: Vec<Scalar<Tag>> = this.machine.env_vars.map.values().map(|&ptr| ptr.into()).collect();
|
|
|
|
// Add the trailing null pointer.
|
|
|
|
vars.push(Scalar::from_int(0, this.pointer_size()));
|
|
|
|
// Make an array with all these pointers inside Miri.
|
|
|
|
let tcx = this.tcx;
|
|
|
|
let vars_layout =
|
|
|
|
this.layout_of(tcx.mk_array(tcx.types.usize, vars.len() as u64))?;
|
|
|
|
let vars_place = this.allocate(vars_layout, MiriMemoryKind::Machine.into());
|
|
|
|
for (idx, var) in vars.into_iter().enumerate() {
|
|
|
|
let place = this.mplace_field(vars_place, idx as u64)?;
|
|
|
|
this.write_scalar(var, place.into())?;
|
|
|
|
}
|
|
|
|
this.write_scalar(
|
|
|
|
vars_place.ptr,
|
|
|
|
this.memory.extra.environ.unwrap().into(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|