miri/shims/
foreign_items.rs

1use std::collections::hash_map::Entry;
2use std::io::Write;
3use std::path::Path;
4
5use rustc_abi::{Align, AlignFromBytesError, Size};
6use rustc_apfloat::Float;
7use rustc_ast::expand::allocator::alloc_error_handler_name;
8use rustc_hir::def::DefKind;
9use rustc_hir::def_id::CrateNum;
10use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
11use rustc_middle::mir::interpret::AllocInit;
12use rustc_middle::ty::Ty;
13use rustc_middle::{mir, ty};
14use rustc_span::Symbol;
15use rustc_target::callconv::{Conv, FnAbi};
16
17use self::helpers::{ToHost, ToSoft};
18use super::alloc::EvalContextExt as _;
19use super::backtrace::EvalContextExt as _;
20use crate::*;
21
22/// Type of dynamic symbols (for `dlsym` et al)
23#[derive(Debug, Copy, Clone)]
24pub struct DynSym(Symbol);
25
26#[expect(clippy::should_implement_trait)]
27impl DynSym {
28    pub fn from_str(name: &str) -> Self {
29        DynSym(Symbol::intern(name))
30    }
31}
32
33impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
34pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
35    /// Emulates calling a foreign item, failing if the item is not supported.
36    /// This function will handle `goto_block` if needed.
37    /// Returns Ok(None) if the foreign item was completely handled
38    /// by this function.
39    /// Returns Ok(Some(body)) if processing the foreign item
40    /// is delegated to another function.
41    fn emulate_foreign_item(
42        &mut self,
43        link_name: Symbol,
44        abi: &FnAbi<'tcx, Ty<'tcx>>,
45        args: &[OpTy<'tcx>],
46        dest: &MPlaceTy<'tcx>,
47        ret: Option<mir::BasicBlock>,
48        unwind: mir::UnwindAction,
49    ) -> InterpResult<'tcx, Option<(&'tcx mir::Body<'tcx>, ty::Instance<'tcx>)>> {
50        let this = self.eval_context_mut();
51
52        // Some shims forward to other MIR bodies.
53        match link_name.as_str() {
54            "__rust_alloc_error_handler" => {
55                // Forward to the right symbol that implements this function.
56                let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
57                    // in real code, this symbol does not exist without an allocator
58                    throw_unsup_format!(
59                        "`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
60                    );
61                };
62                let name = alloc_error_handler_name(handler_kind);
63                let handler = this
64                    .lookup_exported_symbol(Symbol::intern(name))?
65                    .expect("missing alloc error handler symbol");
66                return interp_ok(Some(handler));
67            }
68            _ => {}
69        }
70
71        // The rest either implements the logic, or falls back to `lookup_exported_symbol`.
72        match this.emulate_foreign_item_inner(link_name, abi, args, dest)? {
73            EmulateItemResult::NeedsReturn => {
74                trace!("{:?}", this.dump_place(&dest.clone().into()));
75                this.return_to_block(ret)?;
76            }
77            EmulateItemResult::NeedsUnwind => {
78                // Jump to the unwind block to begin unwinding.
79                this.unwind_to_block(unwind)?;
80            }
81            EmulateItemResult::AlreadyJumped => (),
82            EmulateItemResult::NotSupported => {
83                if let Some(body) = this.lookup_exported_symbol(link_name)? {
84                    return interp_ok(Some(body));
85                }
86
87                throw_machine_stop!(TerminationInfo::UnsupportedForeignItem(format!(
88                    "can't call foreign function `{link_name}` on OS `{os}`",
89                    os = this.tcx.sess.target.os,
90                )));
91            }
92        }
93
94        interp_ok(None)
95    }
96
97    fn is_dyn_sym(&self, name: &str) -> bool {
98        let this = self.eval_context_ref();
99        match this.tcx.sess.target.os.as_ref() {
100            os if this.target_os_is_unix() => shims::unix::foreign_items::is_dyn_sym(name, os),
101            "wasi" => shims::wasi::foreign_items::is_dyn_sym(name),
102            "windows" => shims::windows::foreign_items::is_dyn_sym(name),
103            _ => false,
104        }
105    }
106
107    /// Emulates a call to a `DynSym`.
108    fn emulate_dyn_sym(
109        &mut self,
110        sym: DynSym,
111        abi: &FnAbi<'tcx, Ty<'tcx>>,
112        args: &[OpTy<'tcx>],
113        dest: &MPlaceTy<'tcx>,
114        ret: Option<mir::BasicBlock>,
115        unwind: mir::UnwindAction,
116    ) -> InterpResult<'tcx> {
117        let res = self.emulate_foreign_item(sym.0, abi, args, dest, ret, unwind)?;
118        assert!(res.is_none(), "DynSyms that delegate are not supported");
119        interp_ok(())
120    }
121
122    /// Lookup the body of a function that has `link_name` as the symbol name.
123    fn lookup_exported_symbol(
124        &mut self,
125        link_name: Symbol,
126    ) -> InterpResult<'tcx, Option<(&'tcx mir::Body<'tcx>, ty::Instance<'tcx>)>> {
127        let this = self.eval_context_mut();
128        let tcx = this.tcx.tcx;
129
130        // If the result was cached, just return it.
131        // (Cannot use `or_insert` since the code below might have to throw an error.)
132        let entry = this.machine.exported_symbols_cache.entry(link_name);
133        let instance = *match entry {
134            Entry::Occupied(e) => e.into_mut(),
135            Entry::Vacant(e) => {
136                // Find it if it was not cached.
137                let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
138                helpers::iter_exported_symbols(tcx, |cnum, def_id| {
139                    let attrs = tcx.codegen_fn_attrs(def_id);
140                    let symbol_name = if let Some(export_name) = attrs.export_name {
141                        export_name
142                    } else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
143                        tcx.item_name(def_id)
144                    } else {
145                        // Skip over items without an explicitly defined symbol name.
146                        return interp_ok(());
147                    };
148                    if symbol_name == link_name {
149                        if let Some((original_instance, original_cnum)) = instance_and_crate {
150                            // Make sure we are consistent wrt what is 'first' and 'second'.
151                            let original_span = tcx.def_span(original_instance.def_id()).data();
152                            let span = tcx.def_span(def_id).data();
153                            if original_span < span {
154                                throw_machine_stop!(TerminationInfo::MultipleSymbolDefinitions {
155                                    link_name,
156                                    first: original_span,
157                                    first_crate: tcx.crate_name(original_cnum),
158                                    second: span,
159                                    second_crate: tcx.crate_name(cnum),
160                                });
161                            } else {
162                                throw_machine_stop!(TerminationInfo::MultipleSymbolDefinitions {
163                                    link_name,
164                                    first: span,
165                                    first_crate: tcx.crate_name(cnum),
166                                    second: original_span,
167                                    second_crate: tcx.crate_name(original_cnum),
168                                });
169                            }
170                        }
171                        if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
172                            throw_ub_format!(
173                                "attempt to call an exported symbol that is not defined as a function"
174                            );
175                        }
176                        instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
177                    }
178                    interp_ok(())
179                })?;
180
181                e.insert(instance_and_crate.map(|ic| ic.0))
182            }
183        };
184        match instance {
185            None => interp_ok(None), // no symbol with this name
186            Some(instance) => interp_ok(Some((this.load_mir(instance.def, None)?, instance))),
187        }
188    }
189}
190
191impl<'tcx> EvalContextExtPriv<'tcx> for crate::MiriInterpCx<'tcx> {}
192trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
193    /// Check some basic requirements for this allocation request:
194    /// non-zero size, power-of-two alignment.
195    fn check_rustc_alloc_request(&self, size: u64, align: u64) -> InterpResult<'tcx> {
196        let this = self.eval_context_ref();
197        if size == 0 {
198            throw_ub_format!("creating allocation with size 0");
199        }
200        if size > this.max_size_of_val().bytes() {
201            throw_ub_format!("creating an allocation larger than half the address space");
202        }
203        if let Err(e) = Align::from_bytes(align) {
204            match e {
205                AlignFromBytesError::TooLarge(_) => {
206                    throw_unsup_format!(
207                        "creating allocation with alignment {align} exceeding rustc's maximum \
208                         supported value"
209                    );
210                }
211                AlignFromBytesError::NotPowerOfTwo(_) => {
212                    throw_ub_format!("creating allocation with non-power-of-two alignment {align}");
213                }
214            }
215        }
216
217        interp_ok(())
218    }
219
220    fn emulate_foreign_item_inner(
221        &mut self,
222        link_name: Symbol,
223        abi: &FnAbi<'tcx, Ty<'tcx>>,
224        args: &[OpTy<'tcx>],
225        dest: &MPlaceTy<'tcx>,
226    ) -> InterpResult<'tcx, EmulateItemResult> {
227        let this = self.eval_context_mut();
228
229        // First deal with any external C functions in linked .so file.
230        #[cfg(unix)]
231        if this.machine.native_lib.as_ref().is_some() {
232            use crate::shims::native_lib::EvalContextExt as _;
233            // An Ok(false) here means that the function being called was not exported
234            // by the specified `.so` file; we should continue and check if it corresponds to
235            // a provided shim.
236            if this.call_native_fn(link_name, dest, args)? {
237                return interp_ok(EmulateItemResult::NeedsReturn);
238            }
239        }
240        // When adding a new shim, you should follow the following pattern:
241        // ```
242        // "shim_name" => {
243        //     let [arg1, arg2, arg3] = this.check_shim(abi, Conv::::C , link_name, args)?;
244        //     let result = this.shim_name(arg1, arg2, arg3)?;
245        //     this.write_scalar(result, dest)?;
246        // }
247        // ```
248        // and then define `shim_name` as a helper function in an extension trait in a suitable file
249        // (see e.g. `unix/fs.rs`):
250        // ```
251        // fn shim_name(
252        //     &mut self,
253        //     arg1: &OpTy<'tcx>,
254        //     arg2: &OpTy<'tcx>,
255        //     arg3: &OpTy<'tcx>,
256        //     arg4: &OpTy<'tcx>)
257        // -> InterpResult<'tcx, Scalar> {
258        //     let this = self.eval_context_mut();
259        //
260        //     // First thing: load all the arguments. Details depend on the shim.
261        //     let arg1 = this.read_scalar(arg1)?.to_u32()?;
262        //     let arg2 = this.read_pointer(arg2)?; // when you need to work with the pointer directly
263        //     let arg3 = this.deref_pointer_as(arg3, this.libc_ty_layout("some_libc_struct"))?; // when you want to load/store
264        //         // through the pointer and supply the type information yourself
265        //     let arg4 = this.deref_pointer(arg4)?; // when you want to load/store through the pointer and trust
266        //         // the user-given type (which you shouldn't usually do)
267        //
268        //     // ...
269        //
270        //     interp_ok(Scalar::from_u32(42))
271        // }
272        // ```
273        // You might find existing shims not following this pattern, most
274        // likely because they predate it or because for some reason they cannot be made to fit.
275
276        // Here we dispatch all the shims for foreign functions. If you have a platform specific
277        // shim, add it to the corresponding submodule.
278        match link_name.as_str() {
279            // Miri-specific extern functions
280            "miri_start_unwind" => {
281                let [payload] = this.check_shim(abi, Conv::Rust, link_name, args)?;
282                this.handle_miri_start_unwind(payload)?;
283                return interp_ok(EmulateItemResult::NeedsUnwind);
284            }
285            "miri_run_provenance_gc" => {
286                let [] = this.check_shim(abi, Conv::Rust, link_name, args)?;
287                this.run_provenance_gc();
288            }
289            "miri_get_alloc_id" => {
290                let [ptr] = this.check_shim(abi, Conv::Rust, link_name, args)?;
291                let ptr = this.read_pointer(ptr)?;
292                let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr, 0).map_err_kind(|_e| {
293                    err_machine_stop!(TerminationInfo::Abort(format!(
294                        "pointer passed to `miri_get_alloc_id` must not be dangling, got {ptr:?}"
295                    )))
296                })?;
297                this.write_scalar(Scalar::from_u64(alloc_id.0.get()), dest)?;
298            }
299            "miri_print_borrow_state" => {
300                let [id, show_unnamed] = this.check_shim(abi, Conv::Rust, link_name, args)?;
301                let id = this.read_scalar(id)?.to_u64()?;
302                let show_unnamed = this.read_scalar(show_unnamed)?.to_bool()?;
303                if let Some(id) = std::num::NonZero::new(id).map(AllocId)
304                    && this.get_alloc_info(id).kind == AllocKind::LiveData
305                {
306                    this.print_borrow_state(id, show_unnamed)?;
307                } else {
308                    eprintln!("{id} is not the ID of a live data allocation");
309                }
310            }
311            "miri_pointer_name" => {
312                // This associates a name to a tag. Very useful for debugging, and also makes
313                // tests more strict.
314                let [ptr, nth_parent, name] = this.check_shim(abi, Conv::Rust, link_name, args)?;
315                let ptr = this.read_pointer(ptr)?;
316                let nth_parent = this.read_scalar(nth_parent)?.to_u8()?;
317                let name = this.read_immediate(name)?;
318
319                let name = this.read_byte_slice(&name)?;
320                // We must make `name` owned because we need to
321                // end the shared borrow from `read_byte_slice` before we can
322                // start the mutable borrow for `give_pointer_debug_name`.
323                let name = String::from_utf8_lossy(name).into_owned();
324                this.give_pointer_debug_name(ptr, nth_parent, &name)?;
325            }
326            "miri_static_root" => {
327                let [ptr] = this.check_shim(abi, Conv::Rust, link_name, args)?;
328                let ptr = this.read_pointer(ptr)?;
329                let (alloc_id, offset, _) = this.ptr_get_alloc_id(ptr, 0)?;
330                if offset != Size::ZERO {
331                    throw_unsup_format!(
332                        "pointer passed to `miri_static_root` must point to beginning of an allocated block"
333                    );
334                }
335                this.machine.static_roots.push(alloc_id);
336            }
337            "miri_host_to_target_path" => {
338                let [ptr, out, out_size] = this.check_shim(abi, Conv::Rust, link_name, args)?;
339                let ptr = this.read_pointer(ptr)?;
340                let out = this.read_pointer(out)?;
341                let out_size = this.read_scalar(out_size)?.to_target_usize(this)?;
342
343                // The host affects program behavior here, so this requires isolation to be disabled.
344                this.check_no_isolation("`miri_host_to_target_path`")?;
345
346                // We read this as a plain OsStr and write it as a path, which will convert it to the target.
347                let path = this.read_os_str_from_c_str(ptr)?.to_owned();
348                let (success, needed_size) =
349                    this.write_path_to_c_str(Path::new(&path), out, out_size)?;
350                // Return value: 0 on success, otherwise the size it would have needed.
351                this.write_int(if success { 0 } else { needed_size }, dest)?;
352            }
353            // Obtains the size of a Miri backtrace. See the README for details.
354            "miri_backtrace_size" => {
355                this.handle_miri_backtrace_size(abi, link_name, args, dest)?;
356            }
357            // Obtains a Miri backtrace. See the README for details.
358            "miri_get_backtrace" => {
359                // `check_shim` happens inside `handle_miri_get_backtrace`.
360                this.handle_miri_get_backtrace(abi, link_name, args)?;
361            }
362            // Resolves a Miri backtrace frame. See the README for details.
363            "miri_resolve_frame" => {
364                // `check_shim` happens inside `handle_miri_resolve_frame`.
365                this.handle_miri_resolve_frame(abi, link_name, args, dest)?;
366            }
367            // Writes the function and file names of a Miri backtrace frame into a user provided buffer. See the README for details.
368            "miri_resolve_frame_names" => {
369                this.handle_miri_resolve_frame_names(abi, link_name, args)?;
370            }
371            // Writes some bytes to the interpreter's stdout/stderr. See the
372            // README for details.
373            "miri_write_to_stdout" | "miri_write_to_stderr" => {
374                let [msg] = this.check_shim(abi, Conv::Rust, link_name, args)?;
375                let msg = this.read_immediate(msg)?;
376                let msg = this.read_byte_slice(&msg)?;
377                // Note: we're ignoring errors writing to host stdout/stderr.
378                let _ignore = match link_name.as_str() {
379                    "miri_write_to_stdout" => std::io::stdout().write_all(msg),
380                    "miri_write_to_stderr" => std::io::stderr().write_all(msg),
381                    _ => unreachable!(),
382                };
383            }
384            // Promises that a pointer has a given symbolic alignment.
385            "miri_promise_symbolic_alignment" => {
386                use rustc_abi::AlignFromBytesError;
387
388                let [ptr, align] = this.check_shim(abi, Conv::Rust, link_name, args)?;
389                let ptr = this.read_pointer(ptr)?;
390                let align = this.read_target_usize(align)?;
391                if !align.is_power_of_two() {
392                    throw_unsup_format!(
393                        "`miri_promise_symbolic_alignment`: alignment must be a power of 2, got {align}"
394                    );
395                }
396                let align = Align::from_bytes(align).unwrap_or_else(|err| {
397                    match err {
398                        AlignFromBytesError::NotPowerOfTwo(_) => unreachable!(),
399                        // When the alignment is a power of 2 but too big, clamp it to MAX.
400                        AlignFromBytesError::TooLarge(_) => Align::MAX,
401                    }
402                });
403                let (_, addr) = ptr.into_parts(); // we know the offset is absolute
404                // Cannot panic since `align` is a power of 2 and hence non-zero.
405                if addr.bytes().strict_rem(align.bytes()) != 0 {
406                    throw_unsup_format!(
407                        "`miri_promise_symbolic_alignment`: pointer is not actually aligned"
408                    );
409                }
410                if let Ok((alloc_id, offset, ..)) = this.ptr_try_get_alloc_id(ptr, 0) {
411                    let alloc_align = this.get_alloc_info(alloc_id).align;
412                    // If the newly promised alignment is bigger than the native alignment of this
413                    // allocation, and bigger than the previously promised alignment, then set it.
414                    if align > alloc_align
415                        && this
416                            .machine
417                            .symbolic_alignment
418                            .get_mut()
419                            .get(&alloc_id)
420                            .is_none_or(|&(_, old_align)| align > old_align)
421                    {
422                        this.machine.symbolic_alignment.get_mut().insert(alloc_id, (offset, align));
423                    }
424                }
425            }
426
427            // Aborting the process.
428            "exit" => {
429                let [code] = this.check_shim(abi, Conv::C, link_name, args)?;
430                let code = this.read_scalar(code)?.to_i32()?;
431                throw_machine_stop!(TerminationInfo::Exit { code, leak_check: false });
432            }
433            "abort" => {
434                let [] = this.check_shim(abi, Conv::C, link_name, args)?;
435                throw_machine_stop!(TerminationInfo::Abort(
436                    "the program aborted execution".to_owned()
437                ))
438            }
439
440            // Standard C allocation
441            "malloc" => {
442                let [size] = this.check_shim(abi, Conv::C, link_name, args)?;
443                let size = this.read_target_usize(size)?;
444                if size <= this.max_size_of_val().bytes() {
445                    let res = this.malloc(size, AllocInit::Uninit)?;
446                    this.write_pointer(res, dest)?;
447                } else {
448                    // If this does not fit in an isize, return null and, on Unix, set errno.
449                    if this.target_os_is_unix() {
450                        this.set_last_error(LibcError("ENOMEM"))?;
451                    }
452                    this.write_null(dest)?;
453                }
454            }
455            "calloc" => {
456                let [items, elem_size] = this.check_shim(abi, Conv::C, link_name, args)?;
457                let items = this.read_target_usize(items)?;
458                let elem_size = this.read_target_usize(elem_size)?;
459                if let Some(size) = this.compute_size_in_bytes(Size::from_bytes(elem_size), items) {
460                    let res = this.malloc(size.bytes(), AllocInit::Zero)?;
461                    this.write_pointer(res, dest)?;
462                } else {
463                    // On size overflow, return null and, on Unix, set errno.
464                    if this.target_os_is_unix() {
465                        this.set_last_error(LibcError("ENOMEM"))?;
466                    }
467                    this.write_null(dest)?;
468                }
469            }
470            "free" => {
471                let [ptr] = this.check_shim(abi, Conv::C, link_name, args)?;
472                let ptr = this.read_pointer(ptr)?;
473                this.free(ptr)?;
474            }
475            "realloc" => {
476                let [old_ptr, new_size] = this.check_shim(abi, Conv::C, link_name, args)?;
477                let old_ptr = this.read_pointer(old_ptr)?;
478                let new_size = this.read_target_usize(new_size)?;
479                if new_size <= this.max_size_of_val().bytes() {
480                    let res = this.realloc(old_ptr, new_size)?;
481                    this.write_pointer(res, dest)?;
482                } else {
483                    // If this does not fit in an isize, return null and, on Unix, set errno.
484                    if this.target_os_is_unix() {
485                        this.set_last_error(LibcError("ENOMEM"))?;
486                    }
487                    this.write_null(dest)?;
488                }
489            }
490
491            // Rust allocation
492            "__rust_alloc" | "miri_alloc" => {
493                let default = |ecx: &mut MiriInterpCx<'tcx>| {
494                    // Only call `check_shim` when `#[global_allocator]` isn't used. When that
495                    // macro is used, we act like no shim exists, so that the exported function can run.
496                    let [size, align] = ecx.check_shim(abi, Conv::Rust, link_name, args)?;
497                    let size = ecx.read_target_usize(size)?;
498                    let align = ecx.read_target_usize(align)?;
499
500                    ecx.check_rustc_alloc_request(size, align)?;
501
502                    let memory_kind = match link_name.as_str() {
503                        "__rust_alloc" => MiriMemoryKind::Rust,
504                        "miri_alloc" => MiriMemoryKind::Miri,
505                        _ => unreachable!(),
506                    };
507
508                    let ptr = ecx.allocate_ptr(
509                        Size::from_bytes(size),
510                        Align::from_bytes(align).unwrap(),
511                        memory_kind.into(),
512                        AllocInit::Uninit,
513                    )?;
514
515                    ecx.write_pointer(ptr, dest)
516                };
517
518                match link_name.as_str() {
519                    "__rust_alloc" => return this.emulate_allocator(default),
520                    "miri_alloc" => {
521                        default(this)?;
522                        return interp_ok(EmulateItemResult::NeedsReturn);
523                    }
524                    _ => unreachable!(),
525                }
526            }
527            "__rust_alloc_zeroed" => {
528                return this.emulate_allocator(|this| {
529                    // See the comment for `__rust_alloc` why `check_shim` is only called in the
530                    // default case.
531                    let [size, align] = this.check_shim(abi, Conv::Rust, link_name, args)?;
532                    let size = this.read_target_usize(size)?;
533                    let align = this.read_target_usize(align)?;
534
535                    this.check_rustc_alloc_request(size, align)?;
536
537                    let ptr = this.allocate_ptr(
538                        Size::from_bytes(size),
539                        Align::from_bytes(align).unwrap(),
540                        MiriMemoryKind::Rust.into(),
541                        AllocInit::Zero,
542                    )?;
543                    this.write_pointer(ptr, dest)
544                });
545            }
546            "__rust_dealloc" | "miri_dealloc" => {
547                let default = |ecx: &mut MiriInterpCx<'tcx>| {
548                    // See the comment for `__rust_alloc` why `check_shim` is only called in the
549                    // default case.
550                    let [ptr, old_size, align] =
551                        ecx.check_shim(abi, Conv::Rust, link_name, args)?;
552                    let ptr = ecx.read_pointer(ptr)?;
553                    let old_size = ecx.read_target_usize(old_size)?;
554                    let align = ecx.read_target_usize(align)?;
555
556                    let memory_kind = match link_name.as_str() {
557                        "__rust_dealloc" => MiriMemoryKind::Rust,
558                        "miri_dealloc" => MiriMemoryKind::Miri,
559                        _ => unreachable!(),
560                    };
561
562                    // No need to check old_size/align; we anyway check that they match the allocation.
563                    ecx.deallocate_ptr(
564                        ptr,
565                        Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
566                        memory_kind.into(),
567                    )
568                };
569
570                match link_name.as_str() {
571                    "__rust_dealloc" => {
572                        return this.emulate_allocator(default);
573                    }
574                    "miri_dealloc" => {
575                        default(this)?;
576                        return interp_ok(EmulateItemResult::NeedsReturn);
577                    }
578                    _ => unreachable!(),
579                }
580            }
581            "__rust_realloc" => {
582                return this.emulate_allocator(|this| {
583                    // See the comment for `__rust_alloc` why `check_shim` is only called in the
584                    // default case.
585                    let [ptr, old_size, align, new_size] =
586                        this.check_shim(abi, Conv::Rust, link_name, args)?;
587                    let ptr = this.read_pointer(ptr)?;
588                    let old_size = this.read_target_usize(old_size)?;
589                    let align = this.read_target_usize(align)?;
590                    let new_size = this.read_target_usize(new_size)?;
591                    // No need to check old_size; we anyway check that they match the allocation.
592
593                    this.check_rustc_alloc_request(new_size, align)?;
594
595                    let align = Align::from_bytes(align).unwrap();
596                    let new_ptr = this.reallocate_ptr(
597                        ptr,
598                        Some((Size::from_bytes(old_size), align)),
599                        Size::from_bytes(new_size),
600                        align,
601                        MiriMemoryKind::Rust.into(),
602                        AllocInit::Uninit,
603                    )?;
604                    this.write_pointer(new_ptr, dest)
605                });
606            }
607
608            // C memory handling functions
609            "memcmp" => {
610                let [left, right, n] = this.check_shim(abi, Conv::C, link_name, args)?;
611                let left = this.read_pointer(left)?;
612                let right = this.read_pointer(right)?;
613                let n = Size::from_bytes(this.read_target_usize(n)?);
614
615                // C requires that this must always be a valid pointer (C18 §7.1.4).
616                this.ptr_get_alloc_id(left, 0)?;
617                this.ptr_get_alloc_id(right, 0)?;
618
619                let result = {
620                    let left_bytes = this.read_bytes_ptr_strip_provenance(left, n)?;
621                    let right_bytes = this.read_bytes_ptr_strip_provenance(right, n)?;
622
623                    use std::cmp::Ordering::*;
624                    match left_bytes.cmp(right_bytes) {
625                        Less => -1i32,
626                        Equal => 0,
627                        Greater => 1,
628                    }
629                };
630
631                this.write_scalar(Scalar::from_i32(result), dest)?;
632            }
633            "memrchr" => {
634                let [ptr, val, num] = this.check_shim(abi, Conv::C, link_name, args)?;
635                let ptr = this.read_pointer(ptr)?;
636                let val = this.read_scalar(val)?.to_i32()?;
637                let num = this.read_target_usize(num)?;
638                // The docs say val is "interpreted as unsigned char".
639                #[expect(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
640                let val = val as u8;
641
642                // C requires that this must always be a valid pointer (C18 §7.1.4).
643                this.ptr_get_alloc_id(ptr, 0)?;
644
645                if let Some(idx) = this
646                    .read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(num))?
647                    .iter()
648                    .rev()
649                    .position(|&c| c == val)
650                {
651                    let idx = u64::try_from(idx).unwrap();
652                    #[expect(clippy::arithmetic_side_effects)] // idx < num, so this never wraps
653                    let new_ptr = ptr.wrapping_offset(Size::from_bytes(num - idx - 1), this);
654                    this.write_pointer(new_ptr, dest)?;
655                } else {
656                    this.write_null(dest)?;
657                }
658            }
659            "memchr" => {
660                let [ptr, val, num] = this.check_shim(abi, Conv::C, link_name, args)?;
661                let ptr = this.read_pointer(ptr)?;
662                let val = this.read_scalar(val)?.to_i32()?;
663                let num = this.read_target_usize(num)?;
664                // The docs say val is "interpreted as unsigned char".
665                #[expect(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
666                let val = val as u8;
667
668                // C requires that this must always be a valid pointer (C18 §7.1.4).
669                this.ptr_get_alloc_id(ptr, 0)?;
670
671                let idx = this
672                    .read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(num))?
673                    .iter()
674                    .position(|&c| c == val);
675                if let Some(idx) = idx {
676                    let new_ptr = ptr.wrapping_offset(Size::from_bytes(idx as u64), this);
677                    this.write_pointer(new_ptr, dest)?;
678                } else {
679                    this.write_null(dest)?;
680                }
681            }
682            "strlen" => {
683                let [ptr] = this.check_shim(abi, Conv::C, link_name, args)?;
684                let ptr = this.read_pointer(ptr)?;
685                // This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
686                let n = this.read_c_str(ptr)?.len();
687                this.write_scalar(
688                    Scalar::from_target_usize(u64::try_from(n).unwrap(), this),
689                    dest,
690                )?;
691            }
692            "wcslen" => {
693                let [ptr] = this.check_shim(abi, Conv::C, link_name, args)?;
694                let ptr = this.read_pointer(ptr)?;
695                // This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
696                let n = this.read_wchar_t_str(ptr)?.len();
697                this.write_scalar(
698                    Scalar::from_target_usize(u64::try_from(n).unwrap(), this),
699                    dest,
700                )?;
701            }
702            "memcpy" => {
703                let [ptr_dest, ptr_src, n] = this.check_shim(abi, Conv::C, link_name, args)?;
704                let ptr_dest = this.read_pointer(ptr_dest)?;
705                let ptr_src = this.read_pointer(ptr_src)?;
706                let n = this.read_target_usize(n)?;
707
708                // C requires that this must always be a valid pointer, even if `n` is zero, so we better check that.
709                // (This is more than Rust requires, so `mem_copy` is not sufficient.)
710                this.ptr_get_alloc_id(ptr_dest, 0)?;
711                this.ptr_get_alloc_id(ptr_src, 0)?;
712
713                this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
714                this.write_pointer(ptr_dest, dest)?;
715            }
716            "strcpy" => {
717                let [ptr_dest, ptr_src] = this.check_shim(abi, Conv::C, link_name, args)?;
718                let ptr_dest = this.read_pointer(ptr_dest)?;
719                let ptr_src = this.read_pointer(ptr_src)?;
720
721                // We use `read_c_str` to determine the amount of data to copy,
722                // and then use `mem_copy` for the actual copy. This means
723                // pointer provenance is preserved by this implementation of `strcpy`.
724                // That is probably overly cautious, but there also is no fundamental
725                // reason to have `strcpy` destroy pointer provenance.
726                // This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
727                let n = this.read_c_str(ptr_src)?.len().strict_add(1);
728                this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
729                this.write_pointer(ptr_dest, dest)?;
730            }
731
732            // math functions (note that there are also intrinsics for some other functions)
733            #[rustfmt::skip]
734            | "cbrtf"
735            | "coshf"
736            | "sinhf"
737            | "tanf"
738            | "tanhf"
739            | "acosf"
740            | "asinf"
741            | "atanf"
742            | "log1pf"
743            | "expm1f"
744            | "tgammaf"
745            | "erff"
746            | "erfcf"
747            => {
748                let [f] = this.check_shim(abi, Conv::C , link_name, args)?;
749                let f = this.read_scalar(f)?.to_f32()?;
750                // Using host floats (but it's fine, these operations do not have guaranteed precision).
751                let f_host = f.to_host();
752                let res = match link_name.as_str() {
753                    "cbrtf" => f_host.cbrt(),
754                    "coshf" => f_host.cosh(),
755                    "sinhf" => f_host.sinh(),
756                    "tanf" => f_host.tan(),
757                    "tanhf" => f_host.tanh(),
758                    "acosf" => f_host.acos(),
759                    "asinf" => f_host.asin(),
760                    "atanf" => f_host.atan(),
761                    "log1pf" => f_host.ln_1p(),
762                    "expm1f" => f_host.exp_m1(),
763                    "tgammaf" => f_host.gamma(),
764                    "erff" => f_host.erf(),
765                    "erfcf" => f_host.erfc(),
766                    _ => bug!(),
767                };
768                let res = res.to_soft();
769                let res = this.adjust_nan(res, &[f]);
770                this.write_scalar(res, dest)?;
771            }
772            #[rustfmt::skip]
773            | "_hypotf"
774            | "hypotf"
775            | "atan2f"
776            | "fdimf"
777            => {
778                let [f1, f2] = this.check_shim(abi, Conv::C , link_name, args)?;
779                let f1 = this.read_scalar(f1)?.to_f32()?;
780                let f2 = this.read_scalar(f2)?.to_f32()?;
781                // underscore case for windows, here and below
782                // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
783                // Using host floats (but it's fine, these operations do not have guaranteed precision).
784                let res = match link_name.as_str() {
785                    "_hypotf" | "hypotf" => f1.to_host().hypot(f2.to_host()).to_soft(),
786                    "atan2f" => f1.to_host().atan2(f2.to_host()).to_soft(),
787                    #[allow(deprecated)]
788                    "fdimf" => f1.to_host().abs_sub(f2.to_host()).to_soft(),
789                    _ => bug!(),
790                };
791                let res = this.adjust_nan(res, &[f1, f2]);
792                this.write_scalar(res, dest)?;
793            }
794            #[rustfmt::skip]
795            | "cbrt"
796            | "cosh"
797            | "sinh"
798            | "tan"
799            | "tanh"
800            | "acos"
801            | "asin"
802            | "atan"
803            | "log1p"
804            | "expm1"
805            | "tgamma"
806            | "erf"
807            | "erfc"
808            => {
809                let [f] = this.check_shim(abi, Conv::C , link_name, args)?;
810                let f = this.read_scalar(f)?.to_f64()?;
811                // Using host floats (but it's fine, these operations do not have guaranteed precision).
812                let f_host = f.to_host();
813                let res = match link_name.as_str() {
814                    "cbrt" => f_host.cbrt(),
815                    "cosh" => f_host.cosh(),
816                    "sinh" => f_host.sinh(),
817                    "tan" => f_host.tan(),
818                    "tanh" => f_host.tanh(),
819                    "acos" => f_host.acos(),
820                    "asin" => f_host.asin(),
821                    "atan" => f_host.atan(),
822                    "log1p" => f_host.ln_1p(),
823                    "expm1" => f_host.exp_m1(),
824                    "tgamma" => f_host.gamma(),
825                    "erf" => f_host.erf(),
826                    "erfc" => f_host.erfc(),
827                    _ => bug!(),
828                };
829                let res = res.to_soft();
830                let res = this.adjust_nan(res, &[f]);
831                this.write_scalar(res, dest)?;
832            }
833            #[rustfmt::skip]
834            | "_hypot"
835            | "hypot"
836            | "atan2"
837            | "fdim"
838            => {
839                let [f1, f2] = this.check_shim(abi, Conv::C , link_name, args)?;
840                let f1 = this.read_scalar(f1)?.to_f64()?;
841                let f2 = this.read_scalar(f2)?.to_f64()?;
842                // underscore case for windows, here and below
843                // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
844                // Using host floats (but it's fine, these operations do not have guaranteed precision).
845                let res = match link_name.as_str() {
846                    "_hypot" | "hypot" => f1.to_host().hypot(f2.to_host()).to_soft(),
847                    "atan2" => f1.to_host().atan2(f2.to_host()).to_soft(),
848                    #[allow(deprecated)]
849                    "fdim" => f1.to_host().abs_sub(f2.to_host()).to_soft(),
850                    _ => bug!(),
851                };
852                let res = this.adjust_nan(res, &[f1, f2]);
853                this.write_scalar(res, dest)?;
854            }
855            #[rustfmt::skip]
856            | "_ldexp"
857            | "ldexp"
858            | "scalbn"
859            => {
860                let [x, exp] = this.check_shim(abi, Conv::C , link_name, args)?;
861                // For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
862                let x = this.read_scalar(x)?.to_f64()?;
863                let exp = this.read_scalar(exp)?.to_i32()?;
864
865                let res = x.scalbn(exp);
866                let res = this.adjust_nan(res, &[x]);
867                this.write_scalar(res, dest)?;
868            }
869            "lgammaf_r" => {
870                let [x, signp] = this.check_shim(abi, Conv::C, link_name, args)?;
871                let x = this.read_scalar(x)?.to_f32()?;
872                let signp = this.deref_pointer_as(signp, this.machine.layouts.i32)?;
873
874                // Using host floats (but it's fine, these operations do not have guaranteed precision).
875                let (res, sign) = x.to_host().ln_gamma();
876                this.write_int(sign, &signp)?;
877                let res = this.adjust_nan(res.to_soft(), &[x]);
878                this.write_scalar(res, dest)?;
879            }
880            "lgamma_r" => {
881                let [x, signp] = this.check_shim(abi, Conv::C, link_name, args)?;
882                let x = this.read_scalar(x)?.to_f64()?;
883                let signp = this.deref_pointer_as(signp, this.machine.layouts.i32)?;
884
885                // Using host floats (but it's fine, these operations do not have guaranteed precision).
886                let (res, sign) = x.to_host().ln_gamma();
887                this.write_int(sign, &signp)?;
888                let res = this.adjust_nan(res.to_soft(), &[x]);
889                this.write_scalar(res, dest)?;
890            }
891
892            // LLVM intrinsics
893            "llvm.prefetch" => {
894                let [p, rw, loc, ty] = this.check_shim(abi, Conv::C, link_name, args)?;
895
896                let _ = this.read_pointer(p)?;
897                let rw = this.read_scalar(rw)?.to_i32()?;
898                let loc = this.read_scalar(loc)?.to_i32()?;
899                let ty = this.read_scalar(ty)?.to_i32()?;
900
901                if ty == 1 {
902                    // Data cache prefetch.
903                    // Notably, we do not have to check the pointer, this operation is never UB!
904
905                    if !matches!(rw, 0 | 1) {
906                        throw_unsup_format!("invalid `rw` value passed to `llvm.prefetch`: {}", rw);
907                    }
908                    if !matches!(loc, 0..=3) {
909                        throw_unsup_format!(
910                            "invalid `loc` value passed to `llvm.prefetch`: {}",
911                            loc
912                        );
913                    }
914                } else {
915                    throw_unsup_format!("unsupported `llvm.prefetch` type argument: {}", ty);
916                }
917            }
918            // Used to implement the x86 `_mm{,256,512}_popcnt_epi{8,16,32,64}` and wasm
919            // `{i,u}8x16_popcnt` functions.
920            name if name.starts_with("llvm.ctpop.v") => {
921                let [op] = this.check_shim(abi, Conv::C, link_name, args)?;
922
923                let (op, op_len) = this.project_to_simd(op)?;
924                let (dest, dest_len) = this.project_to_simd(dest)?;
925
926                assert_eq!(dest_len, op_len);
927
928                for i in 0..dest_len {
929                    let op = this.read_immediate(&this.project_index(&op, i)?)?;
930                    // Use `to_uint` to get a zero-extended `u128`. Those
931                    // extra zeros will not affect `count_ones`.
932                    let res = op.to_scalar().to_uint(op.layout.size)?.count_ones();
933
934                    this.write_scalar(
935                        Scalar::from_uint(res, op.layout.size),
936                        &this.project_index(&dest, i)?,
937                    )?;
938                }
939            }
940
941            // Target-specific shims
942            name if name.starts_with("llvm.x86.")
943                && (this.tcx.sess.target.arch == "x86"
944                    || this.tcx.sess.target.arch == "x86_64") =>
945            {
946                return shims::x86::EvalContextExt::emulate_x86_intrinsic(
947                    this, link_name, abi, args, dest,
948                );
949            }
950            // FIXME: Move these to an `arm` submodule.
951            "llvm.aarch64.isb" if this.tcx.sess.target.arch == "aarch64" => {
952                let [arg] = this.check_shim(abi, Conv::C, link_name, args)?;
953                let arg = this.read_scalar(arg)?.to_i32()?;
954                match arg {
955                    // SY ("full system scope")
956                    15 => {
957                        this.yield_active_thread();
958                    }
959                    _ => {
960                        throw_unsup_format!("unsupported llvm.aarch64.isb argument {}", arg);
961                    }
962                }
963            }
964            "llvm.arm.hint" if this.tcx.sess.target.arch == "arm" => {
965                let [arg] = this.check_shim(abi, Conv::C, link_name, args)?;
966                let arg = this.read_scalar(arg)?.to_i32()?;
967                // Note that different arguments might have different target feature requirements.
968                match arg {
969                    // YIELD
970                    1 => {
971                        this.expect_target_feature_for_intrinsic(link_name, "v6")?;
972                        this.yield_active_thread();
973                    }
974                    _ => {
975                        throw_unsup_format!("unsupported llvm.arm.hint argument {}", arg);
976                    }
977                }
978            }
979
980            // Platform-specific shims
981            _ =>
982                return match this.tcx.sess.target.os.as_ref() {
983                    _ if this.target_os_is_unix() =>
984                        shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_inner(
985                            this, link_name, abi, args, dest,
986                        ),
987                    "wasi" =>
988                        shims::wasi::foreign_items::EvalContextExt::emulate_foreign_item_inner(
989                            this, link_name, abi, args, dest,
990                        ),
991                    "windows" =>
992                        shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_inner(
993                            this, link_name, abi, args, dest,
994                        ),
995                    _ => interp_ok(EmulateItemResult::NotSupported),
996                },
997        };
998        // We only fall through to here if we did *not* hit the `_` arm above,
999        // i.e., if we actually emulated the function with one of the shims.
1000        interp_ok(EmulateItemResult::NeedsReturn)
1001    }
1002}