]> git.lizzy.rs Git - rust.git/blob - src/shims/env.rs
Add cargo-miri test for no isolation
[rust.git] / src / shims / env.rs
1 use std::collections::HashMap;
2
3 use rustc::ty::layout::{Size, Align};
4 use rustc_mir::interpret::{Pointer, Memory};
5 use crate::stacked_borrows::Tag;
6 use crate::*;
7
8 #[derive(Default)]
9 pub struct EnvVars {
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>>,
13 }
14
15 impl EnvVars {
16     pub(crate) fn init<'mir, 'tcx>(
17         ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
18         mut excluded_env_vars: Vec<String>,
19     ) {
20         // Exclude `TERM` var to avoid terminfo trying to open the termcap file.
21         excluded_env_vars.push("TERM".to_owned());
22
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);
28                 }
29             }
30         }
31     }
32 }
33
34 fn alloc_env_var<'mir, 'tcx>(
35     name: &[u8],
36     value: &[u8],
37     memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
38 ) -> Pointer<Tag> {
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(),
47     );
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),
53         &tcx,
54     ).unwrap();
55     alloc.write_bytes(&tcx, trailing_zero_ptr, &[0]).unwrap();
56     ptr
57 }
58
59 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
60 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
61     fn getenv(
62         &mut self,
63         name_op: OpTy<'tcx, Tag>,
64     ) -> InterpResult<'tcx, Scalar<Tag>> {
65         let this = self.eval_context_mut();
66
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),
73         })
74     }
75
76     fn setenv(
77         &mut self,
78         name_op: OpTy<'tcx, Tag>,
79         value_op: OpTy<'tcx, Tag>,
80     ) -> InterpResult<'tcx, i32> {
81         let this = self.eval_context_mut();
82
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)?;
86         let mut new = None;
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()));
91             }
92         }
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())?;
97             }
98             Ok(0)
99         } else {
100             Ok(-1)
101         }
102     }
103
104     fn unsetenv(
105         &mut self,
106         name_op: OpTy<'tcx, Tag>,
107     ) -> InterpResult<'tcx, i32> {
108         let this = self.eval_context_mut();
109
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));
116             }
117         }
118         if let Some(old) = success {
119             if let Some(var) = old {
120                 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
121             }
122             Ok(0)
123         } else {
124             Ok(-1)
125         }
126     }
127 }