]> git.lizzy.rs Git - rust.git/blob - src/shims/env.rs
Delegate writing to emulate_foreign_item
[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     map: HashMap<Vec<u8>, Pointer<Tag>>,
11 }
12
13 impl EnvVars {
14     pub(crate) fn init<'mir, 'tcx>(
15         ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
16         communicate: bool,
17     ) {
18         if communicate {
19             for (name, value) in std::env::vars() {
20                 let value = alloc_env_value(value.as_bytes(), ecx.memory_mut());
21                 ecx.machine.env_vars.map.insert(name.into_bytes(), value);
22             }
23         }
24     }
25 }
26
27 fn alloc_env_value<'mir, 'tcx>(
28     bytes: &[u8],
29     memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
30 ) -> Pointer<Tag> {
31     let tcx = {memory.tcx.tcx};
32     let length = bytes.len() as u64;
33     // `+1` for the null terminator.
34     let ptr = memory.allocate(
35         Size::from_bytes(length + 1),
36         Align::from_bytes(1).unwrap(),
37         MiriMemoryKind::Env.into(),
38     );
39     // We just allocated these, so the write cannot fail.
40     let alloc = memory.get_mut(ptr.alloc_id).unwrap();
41     alloc.write_bytes(&tcx, ptr, &bytes).unwrap();
42     let trailing_zero_ptr = ptr.offset(
43         Size::from_bytes(length),
44         &tcx,
45     ).unwrap();
46     alloc.write_bytes(&tcx, trailing_zero_ptr, &[0]).unwrap();
47     ptr
48 }
49
50 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
51 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
52     fn getenv(
53         &mut self,
54         name_op: OpTy<'tcx, Tag>,
55     ) -> InterpResult<'tcx, Scalar<Tag>> {
56         let this = self.eval_context_mut();
57
58         let name_ptr = this.read_scalar(name_op)?.not_undef()?;
59         let name = this.memory().read_c_str(name_ptr)?;
60         Ok(match this.machine.env_vars.map.get(name) {
61             Some(&var) => Scalar::Ptr(var),
62             None => Scalar::ptr_null(&*this.tcx),
63         })
64     }
65
66     fn setenv(
67         &mut self,
68         name_op: OpTy<'tcx, Tag>,
69         value_op: OpTy<'tcx, Tag>,
70     ) -> InterpResult<'tcx, i32> {
71         let this = self.eval_context_mut();
72
73         let name_ptr = this.read_scalar(name_op)?.not_undef()?;
74         let value_ptr = this.read_scalar(value_op)?.not_undef()?;
75         let value = this.memory().read_c_str(value_ptr)?;
76         let mut new = None;
77         if !this.is_null(name_ptr)? {
78             let name = this.memory().read_c_str(name_ptr)?;
79             if !name.is_empty() && !name.contains(&b'=') {
80                 new = Some((name.to_owned(), value.to_owned()));
81             }
82         }
83         if let Some((name, value)) = new {
84             let value_copy = alloc_env_value(&value, this.memory_mut());
85             if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), value_copy) {
86                 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
87             }
88             Ok(0)
89         } else {
90             Ok(-1)
91         }
92     }
93
94     fn unsetenv(
95         &mut self,
96         name_op: OpTy<'tcx, Tag>,
97     ) -> InterpResult<'tcx, i32> {
98         let this = self.eval_context_mut();
99
100         let name_ptr = this.read_scalar(name_op)?.not_undef()?;
101         let mut success = None;
102         if !this.is_null(name_ptr)? {
103             let name = this.memory().read_c_str(name_ptr)?.to_owned();
104             if !name.is_empty() && !name.contains(&b'=') {
105                 success = Some(this.machine.env_vars.map.remove(&name));
106             }
107         }
108         if let Some(old) = success {
109             if let Some(var) = old {
110                 this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
111             }
112             Ok(0)
113         } else {
114             Ok(-1)
115         }
116     }
117 }