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