miri/shims/
foreign_items.rs

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