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;
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 environ(&self) -> Pointer {
54        self.environ.ptr()
55    }
56
57    fn get_ptr(
58        &self,
59        ecx: &InterpCx<'tcx, MiriMachine<'tcx>>,
60        name: &OsStr,
61    ) -> InterpResult<'tcx, Option<Pointer>> {
62        // We don't care about the value as we have the `map` to keep track of everything,
63        // but we do want to do this read so it shows up as a data race.
64        let _vars_ptr = ecx.read_pointer(&self.environ)?;
65        let Some(var_ptr) = self.map.get(name) else {
66            return interp_ok(None);
67        };
68        // The offset is used to strip the "{name}=" part of the string.
69        let var_ptr = var_ptr.wrapping_offset(
70            Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)),
71            ecx,
72        );
73        interp_ok(Some(var_ptr))
74    }
75
76    /// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete
77    /// with the reads of the environment, but returns an [`OsString`] instead of a pointer.
78    pub(crate) fn get(
79        &self,
80        ecx: &InterpCx<'tcx, MiriMachine<'tcx>>,
81        name: &OsStr,
82    ) -> InterpResult<'tcx, Option<OsString>> {
83        let var_ptr = self.get_ptr(ecx, name)?;
84        if let Some(ptr) = var_ptr {
85            let var = ecx.read_os_str_from_c_str(ptr)?;
86            interp_ok(Some(var.to_owned()))
87        } else {
88            interp_ok(None)
89        }
90    }
91}
92
93fn alloc_env_var<'tcx>(
94    ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
95    name: &OsStr,
96    value: &OsStr,
97) -> InterpResult<'tcx, Pointer> {
98    let mut name_osstring = name.to_os_string();
99    name_osstring.push("=");
100    name_osstring.push(value);
101    ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())
102}
103
104/// Allocates an `environ` block with the given list of pointers.
105fn alloc_environ_block<'tcx>(
106    ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
107    mut vars: IndexVec<FieldIdx, Pointer>,
108) -> InterpResult<'tcx, Pointer> {
109    // Add trailing null.
110    vars.push(Pointer::null());
111    // Make an array with all these pointers inside Miri.
112    let vars_layout = ecx.layout_of(Ty::new_array(
113        *ecx.tcx,
114        ecx.machine.layouts.mut_raw_ptr.ty,
115        u64::try_from(vars.len()).unwrap(),
116    ))?;
117    let vars_place = ecx.allocate(vars_layout, MiriMemoryKind::Machine.into())?;
118    for (idx, var) in vars.into_iter_enumerated() {
119        let place = ecx.project_field(&vars_place, idx)?;
120        ecx.write_pointer(var, &place)?;
121    }
122    interp_ok(vars_place.ptr())
123}
124
125impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
126pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
127    fn getenv(&mut self, name_op: &OpTy<'tcx>) -> InterpResult<'tcx, Pointer> {
128        let this = self.eval_context_mut();
129        this.assert_target_os_is_unix("getenv");
130
131        let name_ptr = this.read_pointer(name_op)?;
132        let name = this.read_os_str_from_c_str(name_ptr)?;
133
134        let var_ptr = this.machine.env_vars.unix().get_ptr(this, name)?;
135        interp_ok(var_ptr.unwrap_or_else(Pointer::null))
136    }
137
138    fn setenv(
139        &mut self,
140        name_op: &OpTy<'tcx>,
141        value_op: &OpTy<'tcx>,
142    ) -> InterpResult<'tcx, Scalar> {
143        let this = self.eval_context_mut();
144        this.assert_target_os_is_unix("setenv");
145
146        let name_ptr = this.read_pointer(name_op)?;
147        let value_ptr = this.read_pointer(value_op)?;
148
149        let mut new = None;
150        if !this.ptr_is_null(name_ptr)? {
151            let name = this.read_os_str_from_c_str(name_ptr)?;
152            if !name.is_empty() && !name.to_string_lossy().contains('=') {
153                let value = this.read_os_str_from_c_str(value_ptr)?;
154                new = Some((name.to_owned(), value.to_owned()));
155            }
156        }
157        if let Some((name, value)) = new {
158            let var_ptr = alloc_env_var(this, &name, &value)?;
159            if let Some(var) = this.machine.env_vars.unix_mut().map.insert(name, var_ptr) {
160                this.deallocate_ptr(var, None, MiriMemoryKind::Machine.into())?;
161            }
162            this.update_environ()?;
163            interp_ok(Scalar::from_i32(0)) // return zero on success
164        } else {
165            // name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
166            this.set_last_error_and_return_i32(LibcError("EINVAL"))
167        }
168    }
169
170    fn unsetenv(&mut self, name_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
171        let this = self.eval_context_mut();
172        this.assert_target_os_is_unix("unsetenv");
173
174        let name_ptr = this.read_pointer(name_op)?;
175        let mut success = None;
176        if !this.ptr_is_null(name_ptr)? {
177            let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
178            if !name.is_empty() && !name.to_string_lossy().contains('=') {
179                success = Some(this.machine.env_vars.unix_mut().map.remove(&name));
180            }
181        }
182        if let Some(old) = success {
183            if let Some(var) = old {
184                this.deallocate_ptr(var, None, MiriMemoryKind::Machine.into())?;
185            }
186            this.update_environ()?;
187            interp_ok(Scalar::from_i32(0))
188        } else {
189            // name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
190            this.set_last_error_and_return_i32(LibcError("EINVAL"))
191        }
192    }
193
194    fn getcwd(&mut self, buf_op: &OpTy<'tcx>, size_op: &OpTy<'tcx>) -> InterpResult<'tcx, Pointer> {
195        let this = self.eval_context_mut();
196        this.assert_target_os_is_unix("getcwd");
197
198        let buf = this.read_pointer(buf_op)?;
199        let size = this.read_target_usize(size_op)?;
200
201        if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
202            this.reject_in_isolation("`getcwd`", reject_with)?;
203            this.set_last_error(ErrorKind::PermissionDenied)?;
204            return interp_ok(Pointer::null());
205        }
206
207        // If we cannot get the current directory, we return null
208        match env::current_dir() {
209            Ok(cwd) => {
210                if this.write_path_to_c_str(&cwd, buf, size)?.0 {
211                    return interp_ok(buf);
212                }
213                this.set_last_error(LibcError("ERANGE"))?;
214            }
215            Err(e) => this.set_last_error(e)?,
216        }
217
218        interp_ok(Pointer::null())
219    }
220
221    fn chdir(&mut self, path_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
222        let this = self.eval_context_mut();
223        this.assert_target_os_is_unix("chdir");
224
225        let path = this.read_path_from_c_str(this.read_pointer(path_op)?)?;
226
227        if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
228            this.reject_in_isolation("`chdir`", reject_with)?;
229            return this.set_last_error_and_return_i32(ErrorKind::PermissionDenied);
230        }
231
232        let result = env::set_current_dir(path).map(|()| 0);
233        interp_ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
234    }
235
236    /// Updates the `environ` static.
237    fn update_environ(&mut self) -> InterpResult<'tcx> {
238        let this = self.eval_context_mut();
239        // Deallocate the old environ list.
240        let environ = this.machine.env_vars.unix().environ.clone();
241        let old_vars_ptr = this.read_pointer(&environ)?;
242        this.deallocate_ptr(old_vars_ptr, None, MiriMemoryKind::Machine.into())?;
243
244        // Write the new list.
245        let vals = this.machine.env_vars.unix().map.values().copied().collect();
246        let environ_block = alloc_environ_block(this, vals)?;
247        this.write_pointer(environ_block, &environ)?;
248
249        interp_ok(())
250    }
251
252    fn getpid(&mut self) -> InterpResult<'tcx, Scalar> {
253        let this = self.eval_context_mut();
254        this.assert_target_os_is_unix("getpid");
255
256        // The reason we need to do this wacky of a conversion is because
257        // `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
258        // So we un-do the conversion that stdlib does and turn it back into an i32.
259        // In `Scalar` representation, these are the same, so we don't need to anything else.
260        interp_ok(Scalar::from_u32(this.get_pid()))
261    }
262
263    /// The `gettid`-like function for Unix platforms that take no parameters and return a 32-bit
264    /// integer. It is not always named "gettid".
265    fn unix_gettid(&mut self, link_name: &str) -> InterpResult<'tcx, Scalar> {
266        let this = self.eval_context_ref();
267        this.assert_target_os_is_unix(link_name);
268
269        // For most platforms the return type is an `i32`, but some are unsigned. The TID
270        // will always be positive so we don't need to differentiate.
271        interp_ok(Scalar::from_u32(this.get_current_tid()))
272    }
273
274    /// The Apple-specific `int pthread_threadid_np(pthread_t thread, uint64_t *thread_id)`, which
275    /// allows querying the ID for arbitrary threads, identified by their pthread_t.
276    ///
277    /// API documentation: <https://www.manpagez.com/man/3/pthread_threadid_np/>.
278    fn apple_pthread_threadip_np(
279        &mut self,
280        thread_op: &OpTy<'tcx>,
281        tid_op: &OpTy<'tcx>,
282    ) -> InterpResult<'tcx, Scalar> {
283        let this = self.eval_context_mut();
284        this.assert_target_os("macos", "pthread_threadip_np");
285
286        let tid_dest = this.read_pointer(tid_op)?;
287        if this.ptr_is_null(tid_dest)? {
288            // If NULL is passed, an error is immediately returned
289            return interp_ok(this.eval_libc("EINVAL"));
290        }
291
292        let thread = this.read_scalar(thread_op)?.to_int(this.libc_ty_layout("pthread_t").size)?;
293        let thread = if thread == 0 {
294            // Null thread ID indicates that we are querying the active thread.
295            this.machine.threads.active_thread()
296        } else {
297            // Our pthread_t is just the raw ThreadId.
298            let Ok(thread) = this.thread_id_try_from(thread) else {
299                return interp_ok(this.eval_libc("ESRCH"));
300            };
301            thread
302        };
303
304        let tid = this.get_tid(thread);
305        let tid_dest = this.deref_pointer_as(tid_op, this.machine.layouts.u64)?;
306        this.write_int(tid, &tid_dest)?;
307
308        // Possible errors have been handled, return success.
309        interp_ok(Scalar::from_u32(0))
310    }
311}