miri/shims/unix/
env.rs

1use std::ffi::{OsStr, OsString};
2use std::io::ErrorKind;
3use std::{env, mem};
4
5use rustc_abi::Size;
6use rustc_data_structures::fx::FxHashMap;
7use rustc_middle::ty::Ty;
8use rustc_middle::ty::layout::LayoutOf;
9
10use crate::*;
11
12pub struct UnixEnvVars<'tcx> {
13    /// Stores pointers to the environment variables. These variables must be stored as
14    /// null-terminated target strings (c_str or wide_str) with the `"{name}={value}"` format.
15    map: FxHashMap<OsString, Pointer>,
16
17    /// Place where the `environ` static is stored. Lazily initialized, but then never changes.
18    environ: MPlaceTy<'tcx>,
19}
20
21impl VisitProvenance for UnixEnvVars<'_> {
22    fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
23        let UnixEnvVars { map, environ } = self;
24
25        environ.visit_provenance(visit);
26        for ptr in map.values() {
27            ptr.visit_provenance(visit);
28        }
29    }
30}
31
32impl<'tcx> UnixEnvVars<'tcx> {
33    pub(crate) fn new(
34        ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
35        env_vars: FxHashMap<OsString, OsString>,
36    ) -> InterpResult<'tcx, Self> {
37        // Allocate memory for all these env vars.
38        let mut env_vars_machine = FxHashMap::default();
39        for (name, val) in env_vars.into_iter() {
40            let ptr = alloc_env_var(ecx, &name, &val)?;
41            env_vars_machine.insert(name, ptr);
42        }
43
44        // This is memory backing an extern static, hence `ExternStatic`, not `Env`.
45        let layout = ecx.machine.layouts.mut_raw_ptr;
46        let environ = ecx.allocate(layout, MiriMemoryKind::ExternStatic.into())?;
47        let environ_block = alloc_environ_block(ecx, env_vars_machine.values().copied().collect())?;
48        ecx.write_pointer(environ_block, &environ)?;
49
50        interp_ok(UnixEnvVars { map: env_vars_machine, environ })
51    }
52
53    pub(crate) fn cleanup(ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>) -> InterpResult<'tcx> {
54        // Deallocate individual env vars.
55        let env_vars = mem::take(&mut ecx.machine.env_vars.unix_mut().map);
56        for (_name, ptr) in env_vars {
57            ecx.deallocate_ptr(ptr, None, MiriMemoryKind::Runtime.into())?;
58        }
59        // Deallocate environ var list.
60        let environ = &ecx.machine.env_vars.unix().environ;
61        let old_vars_ptr = ecx.read_pointer(environ)?;
62        ecx.deallocate_ptr(old_vars_ptr, None, MiriMemoryKind::Runtime.into())?;
63
64        interp_ok(())
65    }
66
67    pub(crate) fn environ(&self) -> Pointer {
68        self.environ.ptr()
69    }
70
71    fn get_ptr(
72        &self,
73        ecx: &InterpCx<'tcx, MiriMachine<'tcx>>,
74        name: &OsStr,
75    ) -> InterpResult<'tcx, Option<Pointer>> {
76        // We don't care about the value as we have the `map` to keep track of everything,
77        // but we do want to do this read so it shows up as a data race.
78        let _vars_ptr = ecx.read_pointer(&self.environ)?;
79        let Some(var_ptr) = self.map.get(name) else {
80            return interp_ok(None);
81        };
82        // The offset is used to strip the "{name}=" part of the string.
83        let var_ptr = var_ptr.wrapping_offset(
84            Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)),
85            ecx,
86        );
87        interp_ok(Some(var_ptr))
88    }
89
90    /// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete
91    /// with the reads of the environment, but returns an [`OsString`] instead of a pointer.
92    pub(crate) fn get(
93        &self,
94        ecx: &InterpCx<'tcx, MiriMachine<'tcx>>,
95        name: &OsStr,
96    ) -> InterpResult<'tcx, Option<OsString>> {
97        let var_ptr = self.get_ptr(ecx, name)?;
98        if let Some(ptr) = var_ptr {
99            let var = ecx.read_os_str_from_c_str(ptr)?;
100            interp_ok(Some(var.to_owned()))
101        } else {
102            interp_ok(None)
103        }
104    }
105}
106
107fn alloc_env_var<'tcx>(
108    ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
109    name: &OsStr,
110    value: &OsStr,
111) -> InterpResult<'tcx, Pointer> {
112    let mut name_osstring = name.to_os_string();
113    name_osstring.push("=");
114    name_osstring.push(value);
115    ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Runtime.into())
116}
117
118/// Allocates an `environ` block with the given list of pointers.
119fn alloc_environ_block<'tcx>(
120    ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
121    mut vars: Vec<Pointer>,
122) -> InterpResult<'tcx, Pointer> {
123    // Add trailing null.
124    vars.push(Pointer::null());
125    // Make an array with all these pointers inside Miri.
126    let vars_layout = ecx.layout_of(Ty::new_array(
127        *ecx.tcx,
128        ecx.machine.layouts.mut_raw_ptr.ty,
129        u64::try_from(vars.len()).unwrap(),
130    ))?;
131    let vars_place = ecx.allocate(vars_layout, MiriMemoryKind::Runtime.into())?;
132    for (idx, var) in vars.into_iter().enumerate() {
133        let place = ecx.project_field(&vars_place, idx)?;
134        ecx.write_pointer(var, &place)?;
135    }
136    interp_ok(vars_place.ptr())
137}
138
139impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
140pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
141    fn getenv(&mut self, name_op: &OpTy<'tcx>) -> InterpResult<'tcx, Pointer> {
142        let this = self.eval_context_mut();
143        this.assert_target_os_is_unix("getenv");
144
145        let name_ptr = this.read_pointer(name_op)?;
146        let name = this.read_os_str_from_c_str(name_ptr)?;
147
148        let var_ptr = this.machine.env_vars.unix().get_ptr(this, name)?;
149        interp_ok(var_ptr.unwrap_or_else(Pointer::null))
150    }
151
152    fn setenv(
153        &mut self,
154        name_op: &OpTy<'tcx>,
155        value_op: &OpTy<'tcx>,
156    ) -> InterpResult<'tcx, Scalar> {
157        let this = self.eval_context_mut();
158        this.assert_target_os_is_unix("setenv");
159
160        let name_ptr = this.read_pointer(name_op)?;
161        let value_ptr = this.read_pointer(value_op)?;
162
163        let mut new = None;
164        if !this.ptr_is_null(name_ptr)? {
165            let name = this.read_os_str_from_c_str(name_ptr)?;
166            if !name.is_empty() && !name.to_string_lossy().contains('=') {
167                let value = this.read_os_str_from_c_str(value_ptr)?;
168                new = Some((name.to_owned(), value.to_owned()));
169            }
170        }
171        if let Some((name, value)) = new {
172            let var_ptr = alloc_env_var(this, &name, &value)?;
173            if let Some(var) = this.machine.env_vars.unix_mut().map.insert(name, var_ptr) {
174                this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
175            }
176            this.update_environ()?;
177            interp_ok(Scalar::from_i32(0)) // return zero on success
178        } else {
179            // name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
180            this.set_last_error_and_return_i32(LibcError("EINVAL"))
181        }
182    }
183
184    fn unsetenv(&mut self, name_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
185        let this = self.eval_context_mut();
186        this.assert_target_os_is_unix("unsetenv");
187
188        let name_ptr = this.read_pointer(name_op)?;
189        let mut success = None;
190        if !this.ptr_is_null(name_ptr)? {
191            let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
192            if !name.is_empty() && !name.to_string_lossy().contains('=') {
193                success = Some(this.machine.env_vars.unix_mut().map.remove(&name));
194            }
195        }
196        if let Some(old) = success {
197            if let Some(var) = old {
198                this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
199            }
200            this.update_environ()?;
201            interp_ok(Scalar::from_i32(0))
202        } else {
203            // name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
204            this.set_last_error_and_return_i32(LibcError("EINVAL"))
205        }
206    }
207
208    fn getcwd(&mut self, buf_op: &OpTy<'tcx>, size_op: &OpTy<'tcx>) -> InterpResult<'tcx, Pointer> {
209        let this = self.eval_context_mut();
210        this.assert_target_os_is_unix("getcwd");
211
212        let buf = this.read_pointer(buf_op)?;
213        let size = this.read_target_usize(size_op)?;
214
215        if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
216            this.reject_in_isolation("`getcwd`", reject_with)?;
217            this.set_last_error(ErrorKind::PermissionDenied)?;
218            return interp_ok(Pointer::null());
219        }
220
221        // If we cannot get the current directory, we return null
222        match env::current_dir() {
223            Ok(cwd) => {
224                if this.write_path_to_c_str(&cwd, buf, size)?.0 {
225                    return interp_ok(buf);
226                }
227                this.set_last_error(LibcError("ERANGE"))?;
228            }
229            Err(e) => this.set_last_error(e)?,
230        }
231
232        interp_ok(Pointer::null())
233    }
234
235    fn chdir(&mut self, path_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
236        let this = self.eval_context_mut();
237        this.assert_target_os_is_unix("chdir");
238
239        let path = this.read_path_from_c_str(this.read_pointer(path_op)?)?;
240
241        if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
242            this.reject_in_isolation("`chdir`", reject_with)?;
243            return this.set_last_error_and_return_i32(ErrorKind::PermissionDenied);
244        }
245
246        let result = env::set_current_dir(path).map(|()| 0);
247        interp_ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
248    }
249
250    /// Updates the `environ` static.
251    fn update_environ(&mut self) -> InterpResult<'tcx> {
252        let this = self.eval_context_mut();
253        // Deallocate the old environ list.
254        let environ = this.machine.env_vars.unix().environ.clone();
255        let old_vars_ptr = this.read_pointer(&environ)?;
256        this.deallocate_ptr(old_vars_ptr, None, MiriMemoryKind::Runtime.into())?;
257
258        // Write the new list.
259        let vals = this.machine.env_vars.unix().map.values().copied().collect();
260        let environ_block = alloc_environ_block(this, vals)?;
261        this.write_pointer(environ_block, &environ)?;
262
263        interp_ok(())
264    }
265
266    fn getpid(&mut self) -> InterpResult<'tcx, Scalar> {
267        let this = self.eval_context_mut();
268        this.assert_target_os_is_unix("getpid");
269
270        // The reason we need to do this wacky of a conversion is because
271        // `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
272        // So we un-do the conversion that stdlib does and turn it back into an i32.
273        // In `Scalar` representation, these are the same, so we don't need to anything else.
274        interp_ok(Scalar::from_u32(this.get_pid()))
275    }
276
277    fn linux_gettid(&mut self) -> InterpResult<'tcx, Scalar> {
278        let this = self.eval_context_ref();
279        this.assert_target_os("linux", "gettid");
280
281        let index = this.machine.threads.active_thread().to_u32();
282
283        // Compute a TID for this thread, ensuring that the main thread has PID == TID.
284        let tid = this.get_pid().strict_add(index);
285
286        interp_ok(Scalar::from_u32(tid))
287    }
288}