2019-08-13 11:34:43 -05:00
|
|
|
use std::collections::HashMap;
|
2019-09-18 16:10:13 -05:00
|
|
|
use std::env;
|
2019-08-06 17:40:07 -05:00
|
|
|
|
2019-08-29 15:03:10 -05:00
|
|
|
use rustc::ty::layout::{Size};
|
2019-08-13 11:34:43 -05:00
|
|
|
use rustc_mir::interpret::{Pointer, Memory};
|
|
|
|
use crate::stacked_borrows::Tag;
|
2019-08-06 17:40:07 -05:00
|
|
|
use crate::*;
|
|
|
|
|
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.
|
2019-08-13 11:34:43 -05:00
|
|
|
map: HashMap<Vec<u8>, Pointer<Tag>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnvVars {
|
|
|
|
pub(crate) fn init<'mir, 'tcx>(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
|
2019-08-29 04:07:20 -05:00
|
|
|
mut excluded_env_vars: Vec<String>,
|
2019-08-13 11:34:43 -05:00
|
|
|
) {
|
2019-08-29 04:09:34 -05:00
|
|
|
// Exclude `TERM` var to avoid terminfo trying to open the termcap file.
|
2019-08-29 04:07:20 -05:00
|
|
|
excluded_env_vars.push("TERM".to_owned());
|
2019-08-29 04:09:34 -05:00
|
|
|
|
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) {
|
|
|
|
let var_ptr = alloc_env_var(name.as_bytes(), value.as_bytes(), ecx.memory_mut());
|
|
|
|
ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
|
|
|
|
}
|
2019-08-13 16:17:41 -05:00
|
|
|
}
|
2019-08-13 11:34:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 15:18:11 -05:00
|
|
|
fn alloc_env_var<'mir, 'tcx>(
|
|
|
|
name: &[u8],
|
|
|
|
value: &[u8],
|
2019-08-07 09:10:39 -05:00
|
|
|
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
|
|
|
|
) -> Pointer<Tag> {
|
2019-08-29 15:03:10 -05:00
|
|
|
let mut bytes = name.to_vec();
|
|
|
|
bytes.push(b'=');
|
|
|
|
bytes.extend_from_slice(value);
|
|
|
|
bytes.push(0);
|
|
|
|
memory.allocate_static_bytes(bytes.as_slice(), MiriMemoryKind::Env.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> {
|
|
|
|
fn getenv(
|
|
|
|
&mut self,
|
|
|
|
name_op: OpTy<'tcx, Tag>,
|
2019-08-14 16:48:36 -05:00
|
|
|
) -> 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()?;
|
|
|
|
let name = this.memory().read_c_str(name_ptr)?;
|
|
|
|
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-08-26 15:18:11 -05:00
|
|
|
Some(var_ptr) => Scalar::Ptr(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?),
|
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-08-14 15:44:37 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
|
|
|
|
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
|
|
|
|
let value = this.memory().read_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)? {
|
|
|
|
let name = this.memory().read_c_str(name_ptr)?;
|
|
|
|
if !name.is_empty() && !name.contains(&b'=') {
|
|
|
|
new = Some((name.to_owned(), value.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some((name, value)) = new {
|
2019-08-26 15:18:11 -05:00
|
|
|
let var_ptr = alloc_env_var(&name, &value, this.memory_mut());
|
|
|
|
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
|
2019-08-14 15:44:37 -05:00
|
|
|
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unsetenv(
|
|
|
|
&mut self,
|
|
|
|
name_op: OpTy<'tcx, Tag>,
|
2019-08-14 16:48:36 -05:00
|
|
|
) -> 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)? {
|
|
|
|
let name = this.memory().read_c_str(name_ptr)?.to_owned();
|
|
|
|
if !name.is_empty() && !name.contains(&b'=') {
|
|
|
|
success = Some(this.machine.env_vars.map.remove(&name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(old) = success {
|
|
|
|
if let Some(var) = old {
|
|
|
|
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
|
|
|
|
}
|
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-09-19 10:32:18 -05:00
|
|
|
if this.machine.communicate {
|
|
|
|
let tcx = &{this.tcx.tcx};
|
|
|
|
|
|
|
|
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
|
|
|
|
let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?;
|
|
|
|
// If we cannot get the current directory, we return null
|
|
|
|
if let Ok(cwd) = env::current_dir() {
|
|
|
|
// It is not clear what happens with non-utf8 paths here
|
|
|
|
let mut bytes = cwd.display().to_string().into_bytes();
|
|
|
|
// If the buffer is smaller than the path, we return null
|
|
|
|
if bytes.len() as u64 <= size {
|
|
|
|
// We need `size` bytes exactly
|
|
|
|
bytes.resize(size as usize, 0);
|
|
|
|
this.memory_mut().get_mut(buf.alloc_id)?.write_bytes(tcx, buf, &bytes)?;
|
|
|
|
return Ok(Scalar::Ptr(buf))
|
|
|
|
}
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2019-09-19 10:32:18 -05:00
|
|
|
return Ok(Scalar::ptr_null(&*this.tcx));
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2019-09-19 10:32:18 -05:00
|
|
|
throw_unsup_format!("Function not available when isolation is enabled")
|
2019-09-18 16:10:13 -05:00
|
|
|
}
|
2019-08-14 15:44:37 -05:00
|
|
|
}
|