1 use std::collections::HashMap;
5 use crate::stacked_borrows::Tag;
7 use rustc::ty::layout::Size;
8 use rustc_mir::interpret::{Memory, Pointer};
12 /// Stores pointers to the environment variables. These variables must be stored as
13 /// null-terminated C strings with the `"{name}={value}"` format.
14 map: HashMap<Vec<u8>, Pointer<Tag>>,
18 pub(crate) fn init<'mir, 'tcx>(
19 ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
20 mut excluded_env_vars: Vec<String>,
22 // Exclude `TERM` var to avoid terminfo trying to open the termcap file.
23 excluded_env_vars.push("TERM".to_owned());
25 if ecx.machine.communicate {
26 for (name, value) in env::vars() {
27 if !excluded_env_vars.contains(&name) {
29 alloc_env_var(name.as_bytes(), value.as_bytes(), &mut ecx.memory);
30 ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
37 fn alloc_env_var<'mir, 'tcx>(
40 memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
42 let mut bytes = name.to_vec();
44 bytes.extend_from_slice(value);
46 memory.allocate_static_bytes(bytes.as_slice(), MiriMemoryKind::Env.into())
49 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
50 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
51 fn getenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>> {
52 let this = self.eval_context_mut();
54 let name_ptr = this.read_scalar(name_op)?.not_undef()?;
55 let name = this.memory.read_c_str(name_ptr)?;
56 Ok(match this.machine.env_vars.map.get(name) {
57 // The offset is used to strip the "{name}=" part of the string.
59 Scalar::Ptr(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?)
61 None => Scalar::ptr_null(&*this.tcx),
67 name_op: OpTy<'tcx, Tag>,
68 value_op: OpTy<'tcx, Tag>,
69 ) -> InterpResult<'tcx, i32> {
70 let this = self.eval_context_mut();
72 let name_ptr = this.read_scalar(name_op)?.not_undef()?;
73 let value_ptr = this.read_scalar(value_op)?.not_undef()?;
74 let value = this.memory.read_c_str(value_ptr)?;
76 if !this.is_null(name_ptr)? {
77 let name = this.memory.read_c_str(name_ptr)?;
78 if !name.is_empty() && !name.contains(&b'=') {
79 new = Some((name.to_owned(), value.to_owned()));
82 if let Some((name, value)) = new {
83 let var_ptr = alloc_env_var(&name, &value, &mut this.memory);
84 if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
86 .deallocate(var, None, MiriMemoryKind::Env.into())?;
94 fn unsetenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
95 let this = self.eval_context_mut();
97 let name_ptr = this.read_scalar(name_op)?.not_undef()?;
98 let mut success = None;
99 if !this.is_null(name_ptr)? {
100 let name = this.memory.read_c_str(name_ptr)?.to_owned();
101 if !name.is_empty() && !name.contains(&b'=') {
102 success = Some(this.machine.env_vars.map.remove(&name));
105 if let Some(old) = success {
106 if let Some(var) = old {
108 .deallocate(var, None, MiriMemoryKind::Env.into())?;
118 buf_op: OpTy<'tcx, Tag>,
119 size_op: OpTy<'tcx, Tag>,
120 ) -> InterpResult<'tcx, Scalar<Tag>> {
121 let this = self.eval_context_mut();
123 this.check_no_isolation("getcwd")?;
125 let tcx = &{ this.tcx.tcx };
127 let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
128 let size = this.read_scalar(size_op)?.to_usize(&*tcx)?;
129 // If we cannot get the current directory, we return null
130 match env::current_dir() {
132 // It is not clear what happens with non-utf8 paths here
133 let mut bytes = cwd.display().to_string().into_bytes();
134 // If `size` is smaller or equal than the `bytes.len()`, writing `bytes` plus the
135 // required null terminator to memory using the `buf` pointer would cause an
136 // overflow. The desired behavior in this case is to return null.
137 if (bytes.len() as u64) < size {
138 // We add a `/0` terminator
140 // This is ok because the buffer was strictly larger than `bytes`, so after
141 // adding the null terminator, the buffer size is larger or equal to
142 // `bytes.len()`, meaning that `bytes` actually fit inside tbe buffer.
144 .get_mut(buf.alloc_id)?
145 .write_bytes(tcx, buf, &bytes)?;
146 return Ok(Scalar::Ptr(buf));
148 let erange = this.eval_libc("ERANGE")?;
149 this.set_last_error(erange)?;
151 Err(e) => this.consume_io_error(e)?,
153 Ok(Scalar::ptr_null(&*tcx))
156 fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
157 let this = self.eval_context_mut();
159 this.check_no_isolation("chdir")?;
161 let path_bytes = this
163 .read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
165 let path = Path::new(
166 std::str::from_utf8(path_bytes)
167 .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?,
170 match env::set_current_dir(path) {
173 this.consume_io_error(e)?;