]> git.lizzy.rs Git - rust.git/blob - src/shims/env.rs
Rewrite alloc_env_var
[rust.git] / src / shims / env.rs
1 use std::collections::HashMap;
2
3 use rustc::ty::layout::{Size};
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 mut bytes = name.to_vec();
40     bytes.push(b'=');
41     bytes.extend_from_slice(value);
42     bytes.push(0);
43     memory.allocate_static_bytes(bytes.as_slice(), MiriMemoryKind::Env.into())
44 }
45
46 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
47 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
48     fn getenv(
49         &mut self,
50         name_op: OpTy<'tcx, Tag>,
51     ) -> InterpResult<'tcx, Scalar<Tag>> {
52         let this = self.eval_context_mut();
53
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),
60         })
61     }
62
63     fn setenv(
64         &mut self,
65         name_op: OpTy<'tcx, Tag>,
66         value_op: OpTy<'tcx, Tag>,
67     ) -> InterpResult<'tcx, i32> {
68         let this = self.eval_context_mut();
69
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)?;
73         let mut new = None;
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()));
78             }
79         }
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())?;
84             }
85             Ok(0)
86         } else {
87             Ok(-1)
88         }
89     }
90
91     fn unsetenv(
92         &mut self,
93         name_op: OpTy<'tcx, Tag>,
94     ) -> InterpResult<'tcx, i32> {
95         let this = self.eval_context_mut();
96
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));
103             }
104         }
105         if let Some(old) = success {
106             if let Some(var) = old {
107                 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
108             }
109             Ok(0)
110         } else {
111             Ok(-1)
112         }
113     }
114 }