Skip to main content

miri/shims/unix/
env.rs

1use std::env;
2use std::ffi::{OsStr, OsString};
3use std::io::ErrorKind;
4
5use rustc_abi::{FieldIdx, Size};
6use rustc_data_structures::fx::FxHashMap;
7use rustc_index::IndexVec;
8use rustc_middle::ty::Ty;
9use rustc_target::spec::Os;
10
11use crate::*;
12
13pub struct UnixEnvVars<'tcx> {
14    /// Stores pointers to the environment variables. These variables must be stored as
15    /// null-terminated target strings (c_str or wide_str) with the `"{name}={value}"` format.
16    map: FxHashMap<OsString, Pointer>,
17
18    /// Place where the `environ` static is stored. Lazily initialized, but then never changes.
19    environ: MPlaceTy<'tcx>,
20}
21
22impl VisitProvenance for UnixEnvVars<'_> {
23    fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
24        let UnixEnvVars { map, environ } = self;
25
26        environ.visit_provenance(visit);
27        for ptr in map.values() {
28            ptr.visit_provenance(visit);
29        }
30    }
31}
32
33impl<'tcx> UnixEnvVars<'tcx> {
34    pub(crate) fn new(
35        ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
36        env_vars: FxHashMap<OsString, OsString>,
37    ) -> InterpResult<'tcx, Self> {
38        // Allocate memory for all these env vars.
39        let mut env_vars_machine = FxHashMap::default();
40        for (name, val) in env_vars.into_iter() {
41            let ptr = alloc_env_var(ecx, &name, &val)?;
42            env_vars_machine.insert(name, ptr);
43        }
44
45        // This is memory backing an extern static, hence `ExternStatic`, not `Env`.
46        let layout = ecx.machine.layouts.mut_raw_ptr;
47        let environ = ecx.allocate(layout, MiriMemoryKind::ExternStatic.into())?;
48        let environ_block = alloc_environ_block(ecx, env_vars_machine.values().copied().collect())?;
49        ecx.write_pointer(environ_block, &environ)?;
50
51        interp_ok(UnixEnvVars { map: env_vars_machine, environ })
52    }
53
54    pub(crate) fn environ(&self) -> Pointer {
55        self.environ.ptr()
56    }
57
58    fn get_ptr(
59        &self,
60        ecx: &InterpCx<'tcx, MiriMachine<'tcx>>,
61        name: &OsStr,
62    ) -> InterpResult<'tcx, Option<Pointer>> {
63        // We don't care about the value as we have the `map` to keep track of everything,
64        // but we do want to do this read so it shows up as a data race.
65        let _vars_ptr = ecx.read_pointer(&self.environ)?;
66        let Some(var_ptr) = self.map.get(name) else {
67            return interp_ok(None);
68        };
69        // The offset is used to strip the "{name}=" part of the string.
70        let var_ptr = var_ptr.wrapping_offset(
71            Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)),
72            ecx,
73        );
74        interp_ok(Some(var_ptr))
75    }
76
77    /// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete
78    /// with the reads of the environment, but returns an [`OsString`] instead of a pointer.
79    pub(crate) fn get(
80        &self,
81        ecx: &InterpCx<'tcx, MiriMachine<'tcx>>,
82        name: &OsStr,
83    ) -> InterpResult<'tcx, Option<OsString>> {
84        let var_ptr = self.get_ptr(ecx, name)?;
85        if let Some(ptr) = var_ptr {
86            let var = ecx.read_os_str_from_c_str(ptr)?;
87            interp_ok(Some(var.to_owned()))
88        } else {
89            interp_ok(None)
90        }
91    }
92}
93
94fn alloc_env_var<'tcx>(
95    ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
96    name: &OsStr,
97    value: &OsStr,
98) -> InterpResult<'tcx, Pointer> {
99    let mut name_osstring = name.to_os_string();
100    name_osstring.push("=");
101    name_osstring.push(value);
102    ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())
103}
104
105/// Allocates an `environ` block with the given list of pointers.
106fn alloc_environ_block<'tcx>(
107    ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
108    mut vars: IndexVec<FieldIdx, Pointer>,
109) -> InterpResult<'tcx, Pointer> {
110    // Add trailing null.
111    vars.push(Pointer::null());
112    // Make an array with all these pointers inside Miri.
113    let vars_layout = ecx.layout_of(Ty::new_array(
114        *ecx.tcx,
115        ecx.machine.layouts.mut_raw_ptr.ty,
116        u64::try_from(vars.len()).unwrap(),
117    ))?;
118    let vars_place = ecx.allocate(vars_layout, MiriMemoryKind::Machine.into())?;
119    for (idx, var) in vars.into_iter_enumerated() {
120        let place = ecx.project_field(&vars_place, idx)?;
121        ecx.write_pointer(var, &place)?;
122    }
123    interp_ok(vars_place.ptr())
124}
125
126impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
127pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
128    fn getenv(&mut self, name_op: &OpTy<'tcx>) -> InterpResult<'tcx, Pointer> {
129        let this = self.eval_context_mut();
130        this.assert_target_os_is_unix("getenv");
131
132        let name_ptr = this.read_pointer(name_op)?;
133        let name = this.read_os_str_from_c_str(name_ptr)?;
134
135        let var_ptr = this.machine.env_vars.unix().get_ptr(this, name)?;
136        interp_ok(var_ptr.unwrap_or_else(Pointer::null))
137    }
138
139    fn setenv(
140        &mut self,
141        name_op: &OpTy<'tcx>,
142        value_op: &OpTy<'tcx>,
143    ) -> InterpResult<'tcx, Scalar> {
144        let this = self.eval_context_mut();
145        this.assert_target_os_is_unix("setenv");
146
147        let name_ptr = this.read_pointer(name_op)?;
148        let value_ptr = this.read_pointer(value_op)?;
149
150        let mut new = None;
151        if !this.ptr_is_null(name_ptr)? {
152            let name = this.read_os_str_from_c_str(name_ptr)?;
153            if !name.is_empty() && !name.to_string_lossy().contains('=') {
154                let value = this.read_os_str_from_c_str(value_ptr)?;
155                new = Some((name.to_owned(), value.to_owned()));
156            }
157        }
158        if let Some((name, value)) = new {
159            let var_ptr = alloc_env_var(this, &name, &value)?;
160            if let Some(var) = this.machine.env_vars.unix_mut().map.insert(name, var_ptr) {
161                this.deallocate_ptr(var, None, MiriMemoryKind::Machine.into())?;
162            }
163            this.update_environ()?;
164            interp_ok(Scalar::from_i32(0)) // return zero on success
165        } else {
166            // name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
167            this.set_last_error_and_return_i32(LibcError("EINVAL"))
168        }
169    }
170
171    fn unsetenv(&mut self, name_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
172        let this = self.eval_context_mut();
173        this.assert_target_os_is_unix("unsetenv");
174
175        let name_ptr = this.read_pointer(name_op)?;
176        let mut success = None;
177        if !this.ptr_is_null(name_ptr)? {
178            let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
179            if !name.is_empty() && !name.to_string_lossy().contains('=') {
180                success = Some(this.machine.env_vars.unix_mut().map.remove(&name));
181            }
182        }
183        if let Some(old) = success {
184            if let Some(var) = old {
185                this.deallocate_ptr(var, None, MiriMemoryKind::Machine.into())?;
186            }
187            this.update_environ()?;
188            interp_ok(Scalar::from_i32(0))
189        } else {
190            // name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
191            this.set_last_error_and_return_i32(LibcError("EINVAL"))
192        }
193    }
194
195    fn getcwd(&mut self, buf_op: &OpTy<'tcx>, size_op: &OpTy<'tcx>) -> InterpResult<'tcx, Pointer> {
196        let this = self.eval_context_mut();
197        this.assert_target_os_is_unix("getcwd");
198
199        let buf = this.read_pointer(buf_op)?;
200        let size = this.read_target_usize(size_op)?;
201
202        if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
203            this.reject_in_isolation("`getcwd`", reject_with)?;
204            this.set_last_error(ErrorKind::PermissionDenied)?;
205            return interp_ok(Pointer::null());
206        }
207
208        // If we cannot get the current directory, we return null
209        match env::current_dir() {
210            Ok(cwd) => {
211                if this.write_path_to_c_str(&cwd, buf, size)?.0 {
212                    return interp_ok(buf);
213                }
214                this.set_last_error(LibcError("ERANGE"))?;
215            }
216            Err(e) => this.set_last_error(e)?,
217        }
218
219        interp_ok(Pointer::null())
220    }
221
222    fn chdir(&mut self, path_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
223        let this = self.eval_context_mut();
224        this.assert_target_os_is_unix("chdir");
225
226        let path = this.read_path_from_c_str(this.read_pointer(path_op)?)?;
227
228        if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
229            this.reject_in_isolation("`chdir`", reject_with)?;
230            return this.set_last_error_and_return_i32(ErrorKind::PermissionDenied);
231        }
232
233        let result = env::set_current_dir(path).map(|()| 0);
234        interp_ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
235    }
236
237    /// Updates the `environ` static.
238    fn update_environ(&mut self) -> InterpResult<'tcx> {
239        let this = self.eval_context_mut();
240        // Deallocate the old environ list.
241        let environ = this.machine.env_vars.unix().environ.clone();
242        let old_vars_ptr = this.read_pointer(&environ)?;
243        this.deallocate_ptr(old_vars_ptr, None, MiriMemoryKind::Machine.into())?;
244
245        // Write the new list.
246        let vals = this.machine.env_vars.unix().map.values().copied().collect();
247        let environ_block = alloc_environ_block(this, vals)?;
248        this.write_pointer(environ_block, &environ)?;
249
250        interp_ok(())
251    }
252
253    fn getpid(&mut self) -> InterpResult<'tcx, Scalar> {
254        let this = self.eval_context_mut();
255        this.assert_target_os_is_unix("getpid");
256
257        // The reason we need to do this wacky of a conversion is because
258        // `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
259        // So we un-do the conversion that stdlib does and turn it back into an i32.
260        // In `Scalar` representation, these are the same, so we don't need to anything else.
261        interp_ok(Scalar::from_u32(this.get_pid()))
262    }
263
264    /// The `gettid`-like function for Unix platforms that take no parameters and return a 32-bit
265    /// integer. It is not always named "gettid".
266    fn unix_gettid(&mut self, link_name: &str) -> InterpResult<'tcx, Scalar> {
267        let this = self.eval_context_ref();
268        this.assert_target_os_is_unix(link_name);
269
270        // For most platforms the return type is an `i32`, but some are unsigned. The TID
271        // will always be positive so we don't need to differentiate.
272        interp_ok(Scalar::from_u32(this.get_current_tid()))
273    }
274
275    /// `fields_size`, if present, says how large each field of the struct is.
276    fn uname(
277        &mut self,
278        uname: &OpTy<'tcx>,
279        fields_size: Option<&OpTy<'tcx>>,
280    ) -> InterpResult<'tcx, Scalar> {
281        let this = self.eval_context_mut();
282        this.assert_target_os_is_unix("uname");
283
284        let uname_ptr = this.read_pointer(uname)?;
285        let fields_size = match fields_size {
286            None => None,
287            Some(size) => Some(this.read_scalar(size)?.to_i32()?),
288        };
289
290        if this.ptr_is_null(uname_ptr)? {
291            return this.set_last_error_and_return_i32(LibcError("EFAULT"));
292        }
293
294        let uname = this.deref_pointer_as(uname, this.libc_ty_layout("utsname"))?;
295        let arch = this.machine.tcx.sess.target.arch.desc_symbol();
296        // Values required by POSIX.
297        let mut values = vec![
298            ("sysname", "Miri"),
299            ("nodename", "Miri"),
300            ("release", env!("CARGO_PKG_VERSION")),
301            ("version", concat!("Miri ", env!("CARGO_PKG_VERSION"))),
302            ("machine", arch.as_str()),
303        ];
304        if matches!(this.machine.tcx.sess.target.os, Os::Linux | Os::Android) {
305            values.push(("domainname", "(none)"));
306        }
307
308        for (name, value) in values {
309            let field = this.project_field_named(&uname, name)?;
310            let size = field.layout().layout.size().bytes();
311            if fields_size.is_some_and(|fields_size| u64::try_from(fields_size) != Ok(size)) {
312                throw_unsup_format!(
313                    "the fields size passed to `uname` does not match the type in the libc crate"
314                );
315            }
316            let (written, _) = this.write_c_str(value.as_bytes(), field.ptr(), size)?;
317            assert!(written); // All values should fit.
318        }
319        interp_ok(Scalar::from_i32(0))
320    }
321
322    /// The Apple-specific `int pthread_threadid_np(pthread_t thread, uint64_t *thread_id)`, which
323    /// allows querying the ID for arbitrary threads, identified by their pthread_t.
324    ///
325    /// API documentation: <https://www.manpagez.com/man/3/pthread_threadid_np/>.
326    fn apple_pthread_threadip_np(
327        &mut self,
328        thread_op: &OpTy<'tcx>,
329        tid_op: &OpTy<'tcx>,
330    ) -> InterpResult<'tcx, Scalar> {
331        let this = self.eval_context_mut();
332        this.assert_target_os(Os::MacOs, "pthread_threadip_np");
333
334        let tid_dest = this.read_pointer(tid_op)?;
335        if this.ptr_is_null(tid_dest)? {
336            // If NULL is passed, an error is immediately returned
337            return interp_ok(this.eval_libc("EINVAL"));
338        }
339
340        let thread = this.read_scalar(thread_op)?.to_int(this.libc_ty_layout("pthread_t").size)?;
341        let thread = if thread == 0 {
342            // Null thread ID indicates that we are querying the active thread.
343            this.machine.threads.active_thread()
344        } else {
345            // Our pthread_t is just the raw ThreadId.
346            let Ok(thread) = this.thread_id_try_from(thread) else {
347                return interp_ok(this.eval_libc("ESRCH"));
348            };
349            thread
350        };
351
352        let tid = this.get_tid(thread);
353        let tid_dest = this.deref_pointer_as(tid_op, this.machine.layouts.u64)?;
354        this.write_int(tid, &tid_dest)?;
355
356        // Possible errors have been handled, return success.
357        interp_ok(Scalar::from_u32(0))
358    }
359}