1 use std::collections::HashMap;
3 use rustc::ty::layout::{Size};
4 use rustc_mir::interpret::{Pointer, Memory};
5 use crate::stacked_borrows::Tag;
10 /// Stores pointers to the environment variables. These variables must be stored as
11 /// null-terminated C strings with the `"{name}={value}"` format.
12 map: HashMap<Vec<u8>, Pointer<Tag>>,
16 pub(crate) fn init<'mir, 'tcx>(
17 ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
18 mut excluded_env_vars: Vec<String>,
20 // Exclude `TERM` var to avoid terminfo trying to open the termcap file.
21 excluded_env_vars.push("TERM".to_owned());
23 if ecx.machine.communicate {
24 for (name, value) in std::env::vars() {
25 if !excluded_env_vars.contains(&name) {
26 let var_ptr = alloc_env_var(name.as_bytes(), value.as_bytes(), ecx.memory_mut());
27 ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
34 fn alloc_env_var<'mir, 'tcx>(
37 memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
39 let mut bytes = name.to_vec();
41 bytes.extend_from_slice(value);
43 memory.allocate_static_bytes(bytes.as_slice(), MiriMemoryKind::Env.into())
46 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
47 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
50 name_op: OpTy<'tcx, Tag>,
51 ) -> 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.
58 Some(var_ptr) => Scalar::Ptr(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?),
59 None => Scalar::ptr_null(&*this.tcx),
65 name_op: OpTy<'tcx, Tag>,
66 value_op: OpTy<'tcx, Tag>,
67 ) -> InterpResult<'tcx, i32> {
68 let this = self.eval_context_mut();
70 let name_ptr = this.read_scalar(name_op)?.not_undef()?;
71 let value_ptr = this.read_scalar(value_op)?.not_undef()?;
72 let value = this.memory().read_c_str(value_ptr)?;
74 if !this.is_null(name_ptr)? {
75 let name = this.memory().read_c_str(name_ptr)?;
76 if !name.is_empty() && !name.contains(&b'=') {
77 new = Some((name.to_owned(), value.to_owned()));
80 if let Some((name, value)) = new {
81 let var_ptr = alloc_env_var(&name, &value, this.memory_mut());
82 if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
83 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
93 name_op: OpTy<'tcx, Tag>,
94 ) -> 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 {
107 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;