1 use std::collections::HashMap;
3 use rustc::ty::layout::{Size, Align};
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 bytes = [name, b"=", value].concat();
40 let tcx = {memory.tcx.tcx};
41 let length = bytes.len() as u64;
42 // `+1` for the null terminator.
43 let ptr = memory.allocate(
44 Size::from_bytes(length + 1),
45 Align::from_bytes(1).unwrap(),
46 MiriMemoryKind::Env.into(),
48 // We just allocated these, so the write cannot fail.
49 let alloc = memory.get_mut(ptr.alloc_id).unwrap();
50 alloc.write_bytes(&tcx, ptr, &bytes).unwrap();
51 let trailing_zero_ptr = ptr.offset(
52 Size::from_bytes(length),
55 alloc.write_bytes(&tcx, trailing_zero_ptr, &[0]).unwrap();
59 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
60 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
63 name_op: OpTy<'tcx, Tag>,
64 ) -> InterpResult<'tcx, Scalar<Tag>> {
65 let this = self.eval_context_mut();
67 let name_ptr = this.read_scalar(name_op)?.not_undef()?;
68 let name = this.memory().read_c_str(name_ptr)?;
69 Ok(match this.machine.env_vars.map.get(name) {
70 // The offset is used to strip the "{name}=" part of the string.
71 Some(var_ptr) => Scalar::Ptr(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?),
72 None => Scalar::ptr_null(&*this.tcx),
78 name_op: OpTy<'tcx, Tag>,
79 value_op: OpTy<'tcx, Tag>,
80 ) -> InterpResult<'tcx, i32> {
81 let this = self.eval_context_mut();
83 let name_ptr = this.read_scalar(name_op)?.not_undef()?;
84 let value_ptr = this.read_scalar(value_op)?.not_undef()?;
85 let value = this.memory().read_c_str(value_ptr)?;
87 if !this.is_null(name_ptr)? {
88 let name = this.memory().read_c_str(name_ptr)?;
89 if !name.is_empty() && !name.contains(&b'=') {
90 new = Some((name.to_owned(), value.to_owned()));
93 if let Some((name, value)) = new {
94 let var_ptr = alloc_env_var(&name, &value, this.memory_mut());
95 if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
96 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
106 name_op: OpTy<'tcx, Tag>,
107 ) -> InterpResult<'tcx, i32> {
108 let this = self.eval_context_mut();
110 let name_ptr = this.read_scalar(name_op)?.not_undef()?;
111 let mut success = None;
112 if !this.is_null(name_ptr)? {
113 let name = this.memory().read_c_str(name_ptr)?.to_owned();
114 if !name.is_empty() && !name.contains(&b'=') {
115 success = Some(this.machine.env_vars.map.remove(&name));
118 if let Some(old) = success {
119 if let Some(var) = old {
120 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;