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_session::config::OomStrategy;
16use rustc_span::Symbol;
17use rustc_target::callconv::FnAbi;
18use rustc_target::spec::{Arch, Os};
19
20use super::alloc::EvalContextExt as _;
21use super::backtrace::EvalContextExt as _;
22use crate::concurrency::GenmcEvalContextExt as _;
23use crate::helpers::EvalContextExt as _;
24use crate::*;
25
26#[derive(Debug, Copy, Clone)]
28pub struct DynSym(Symbol);
29
30#[expect(clippy::should_implement_trait)]
31impl DynSym {
32 pub fn from_str(name: &str) -> Self {
33 DynSym(Symbol::intern(name))
34 }
35}
36
37impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
38pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
39 fn emulate_foreign_item(
46 &mut self,
47 link_name: Symbol,
48 abi: &FnAbi<'tcx, Ty<'tcx>>,
49 args: &[OpTy<'tcx>],
50 dest: &PlaceTy<'tcx>,
51 ret: Option<mir::BasicBlock>,
52 unwind: mir::UnwindAction,
53 ) -> InterpResult<'tcx, Option<(&'tcx mir::Body<'tcx>, ty::Instance<'tcx>)>> {
54 let this = self.eval_context_mut();
55
56 if let Some(shim) = this.machine.allocator_shim_symbols.get(&link_name) {
58 match *shim {
59 Either::Left(other_fn) => {
60 let handler = this
61 .lookup_exported_symbol(other_fn)?
62 .expect("missing alloc error handler symbol");
63 return interp_ok(Some(handler));
64 }
65 Either::Right(special) => {
66 this.rust_special_allocator_method(special, link_name, abi, args, dest)?;
67 this.return_to_block(ret)?;
68 return interp_ok(None);
69 }
70 }
71 }
72
73 let dest = this.force_allocation(dest)?;
75
76 match this.emulate_foreign_item_inner(link_name, abi, args, &dest)? {
78 EmulateItemResult::NeedsReturn => {
79 trace!("{:?}", this.dump_place(&dest.clone().into()));
80 this.return_to_block(ret)?;
81 }
82 EmulateItemResult::NeedsUnwind => {
83 this.unwind_to_block(unwind)?;
85 }
86 EmulateItemResult::AlreadyJumped => (),
87 EmulateItemResult::NotSupported => {
88 if let Some(body) = this.lookup_exported_symbol(link_name)? {
89 return interp_ok(Some(body));
90 }
91
92 throw_machine_stop!(TerminationInfo::UnsupportedForeignItem(format!(
93 "can't call foreign function `{link_name}` on OS `{os}`",
94 os = this.tcx.sess.target.os,
95 )));
96 }
97 }
98
99 interp_ok(None)
100 }
101
102 fn is_dyn_sym(&self, name: &str) -> bool {
103 let this = self.eval_context_ref();
104 match &this.tcx.sess.target.os {
105 os if this.target_os_is_unix() => shims::unix::foreign_items::is_dyn_sym(name, os),
106 Os::Windows => shims::windows::foreign_items::is_dyn_sym(name),
107 _ => false,
108 }
109 }
110
111 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 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 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 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 if tcx.is_foreign_item(def_id) {
152 return interp_ok(());
153 }
154 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 match (is_weak, original.is_weak) {
170 (false, true) => {
171 symbol_target = Some(SymbolTarget {
174 instance: ty::Instance::mono(tcx, def_id),
175 cnum,
176 is_weak,
177 });
178 }
179 (true, false) => {
180 }
182 (true, true) | (false, false) => {
183 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 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), 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 fn emulate_foreign_item_inner(
250 &mut self,
251 link_name: Symbol,
252 abi: &FnAbi<'tcx, Ty<'tcx>>,
253 args: &[OpTy<'tcx>],
254 dest: &MPlaceTy<'tcx>,
255 ) -> InterpResult<'tcx, EmulateItemResult> {
256 let this = self.eval_context_mut();
257
258 #[cfg(all(unix, feature = "native-lib"))]
260 if !this.machine.native_lib.is_empty() {
261 use crate::shims::native_lib::EvalContextExt as _;
262 if this.call_native_fn(link_name, dest, args)? {
266 return interp_ok(EmulateItemResult::NeedsReturn);
267 }
268 }
269 match link_name.as_str() {
308 name if name == this.mangle_internal_symbol(NO_ALLOC_SHIM_IS_UNSTABLE) => {
310 let [] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
313 }
314 name if name == this.mangle_internal_symbol(OomStrategy::SYMBOL) => {
315 let [] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
317 let val = this.tcx.sess.opts.unstable_opts.oom.should_panic();
318 this.write_int(val, dest)?;
319 }
320
321 "miri_alloc" => {
323 let [size, align] =
324 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
325 let size = this.read_target_usize(size)?;
326 let align = this.read_target_usize(align)?;
327
328 this.check_rust_alloc_request(size, align)?;
329
330 let ptr = this.allocate_ptr(
331 Size::from_bytes(size),
332 Align::from_bytes(align).unwrap(),
333 MiriMemoryKind::Miri.into(),
334 AllocInit::Uninit,
335 )?;
336
337 this.write_pointer(ptr, dest)?;
338 }
339 "miri_dealloc" => {
340 let [ptr, old_size, align] =
341 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
342 let ptr = this.read_pointer(ptr)?;
343 let old_size = this.read_target_usize(old_size)?;
344 let align = this.read_target_usize(align)?;
345
346 this.deallocate_ptr(
348 ptr,
349 Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
350 MiriMemoryKind::Miri.into(),
351 )?;
352 }
353 "miri_track_alloc" => {
354 let [ptr] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
355 let ptr = this.read_pointer(ptr)?;
356 let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr, 0).map_err_kind(|_e| {
357 err_machine_stop!(TerminationInfo::Abort(format!(
358 "pointer passed to `miri_get_alloc_id` must not be dangling, got {ptr:?}"
359 )))
360 })?;
361 if this.machine.tracked_alloc_ids.insert(alloc_id) {
362 let info = this.get_alloc_info(alloc_id);
363 this.emit_diagnostic(NonHaltingDiagnostic::TrackingAlloc(
364 alloc_id, info.size, info.align,
365 ));
366 }
367 }
368 "miri_start_unwind" => {
369 let [payload] =
370 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
371 this.handle_miri_start_unwind(payload)?;
372 return interp_ok(EmulateItemResult::NeedsUnwind);
373 }
374 "miri_run_provenance_gc" => {
375 let [] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
376 this.run_provenance_gc();
377 }
378 "miri_get_alloc_id" => {
379 let [ptr] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
380 let ptr = this.read_pointer(ptr)?;
381 let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr, 0).map_err_kind(|_e| {
382 err_machine_stop!(TerminationInfo::Abort(format!(
383 "pointer passed to `miri_get_alloc_id` must not be dangling, got {ptr:?}"
384 )))
385 })?;
386 this.write_scalar(Scalar::from_u64(alloc_id.0.get()), dest)?;
387 }
388 "miri_print_borrow_state" => {
389 let [id, show_unnamed] =
390 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
391 let id = this.read_scalar(id)?.to_u64()?;
392 let show_unnamed = this.read_scalar(show_unnamed)?.to_bool()?;
393 if let Some(id) = std::num::NonZero::new(id).map(AllocId)
394 && this.get_alloc_info(id).kind == AllocKind::LiveData
395 {
396 this.print_borrow_state(id, show_unnamed)?;
397 } else {
398 eprintln!("{id} is not the ID of a live data allocation");
399 }
400 }
401 "miri_pointer_name" => {
402 let [ptr, nth_parent, name] =
405 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
406 let ptr = this.read_pointer(ptr)?;
407 let nth_parent = this.read_scalar(nth_parent)?.to_u8()?;
408 let name = this.read_immediate(name)?;
409
410 let name = this.read_byte_slice(&name)?;
411 let name = String::from_utf8_lossy(name).into_owned();
415 this.give_pointer_debug_name(ptr, nth_parent, &name)?;
416 }
417 "miri_static_root" => {
418 let [ptr] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
419 let ptr = this.read_pointer(ptr)?;
420 let (alloc_id, offset, _) = this.ptr_get_alloc_id(ptr, 0)?;
421 if offset != Size::ZERO {
422 throw_unsup_format!(
423 "pointer passed to `miri_static_root` must point to beginning of an allocated block"
424 );
425 }
426 this.machine.static_roots.push(alloc_id);
427 }
428 "miri_host_to_target_path" => {
429 let [ptr, out, out_size] =
430 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
431 let ptr = this.read_pointer(ptr)?;
432 let out = this.read_pointer(out)?;
433 let out_size = this.read_scalar(out_size)?.to_target_usize(this)?;
434
435 this.check_no_isolation("`miri_host_to_target_path`")?;
437
438 let path = this.read_os_str_from_c_str(ptr)?.to_owned();
440 let (success, needed_size) =
441 this.write_path_to_c_str(Path::new(&path), out, out_size)?;
442 this.write_int(if success { 0 } else { needed_size }, dest)?;
444 }
445 "miri_backtrace_size" => {
447 this.handle_miri_backtrace_size(abi, link_name, args, dest)?;
448 }
449 "miri_get_backtrace" => {
451 this.handle_miri_get_backtrace(abi, link_name, args)?;
453 }
454 "miri_resolve_frame" => {
456 this.handle_miri_resolve_frame(abi, link_name, args, dest)?;
458 }
459 "miri_resolve_frame_names" => {
461 this.handle_miri_resolve_frame_names(abi, link_name, args)?;
462 }
463 "miri_write_to_stdout" | "miri_write_to_stderr" => {
466 let [msg] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
467 let msg = this.read_immediate(msg)?;
468 let msg = this.read_byte_slice(&msg)?;
469 let _ignore = match link_name.as_str() {
471 "miri_write_to_stdout" => std::io::stdout().write_all(msg),
472 "miri_write_to_stderr" => std::io::stderr().write_all(msg),
473 _ => unreachable!(),
474 };
475 }
476 "miri_promise_symbolic_alignment" => {
478 use rustc_abi::AlignFromBytesError;
479
480 let [ptr, align] =
481 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
482 let ptr = this.read_pointer(ptr)?;
483 let align = this.read_target_usize(align)?;
484 if !align.is_power_of_two() {
485 throw_unsup_format!(
486 "`miri_promise_symbolic_alignment`: alignment must be a power of 2, got {align}"
487 );
488 }
489 let align = Align::from_bytes(align).unwrap_or_else(|err| {
490 match err {
491 AlignFromBytesError::NotPowerOfTwo(_) => unreachable!(),
492 AlignFromBytesError::TooLarge(_) => Align::MAX,
494 }
495 });
496 let addr = ptr.addr();
497 if addr.bytes().strict_rem(align.bytes()) != 0 {
499 throw_unsup_format!(
500 "`miri_promise_symbolic_alignment`: pointer is not actually aligned"
501 );
502 }
503 if let Ok((alloc_id, offset, ..)) = this.ptr_try_get_alloc_id(ptr, 0) {
504 let alloc_align = this.get_alloc_info(alloc_id).align;
505 if align > alloc_align
508 && this
509 .machine
510 .symbolic_alignment
511 .get_mut()
512 .get(&alloc_id)
513 .is_none_or(|&(_, old_align)| align > old_align)
514 {
515 this.machine.symbolic_alignment.get_mut().insert(alloc_id, (offset, align));
516 }
517 }
518 }
519 "miri_genmc_assume" => {
521 let [condition] =
522 this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
523 if this.machine.data_race.as_genmc_ref().is_some() {
524 this.handle_genmc_verifier_assume(condition)?;
525 } else {
526 throw_unsup_format!("miri_genmc_assume is only supported in GenMC mode")
527 }
528 }
529
530 "exit" => {
532 let [code] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
533 let code = this.read_scalar(code)?.to_i32()?;
534 if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
535 genmc_ctx.handle_exit(
537 this.machine.threads.active_thread(),
538 code,
539 crate::concurrency::ExitType::ExitCalled,
540 )?;
541 todo!(); }
543 throw_machine_stop!(TerminationInfo::Exit { code, leak_check: false });
544 }
545 "abort" => {
546 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
547 throw_machine_stop!(TerminationInfo::Abort(
548 "the program aborted execution".to_owned()
549 ));
550 }
551
552 "malloc" => {
554 let [size] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
555 let size = this.read_target_usize(size)?;
556 if size <= this.max_size_of_val().bytes() {
557 let res = this.malloc(size, AllocInit::Uninit)?;
558 this.write_pointer(res, dest)?;
559 } else {
560 if this.target_os_is_unix() {
562 this.set_last_error(LibcError("ENOMEM"))?;
563 }
564 this.write_null(dest)?;
565 }
566 }
567 "calloc" => {
568 let [items, elem_size] =
569 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
570 let items = this.read_target_usize(items)?;
571 let elem_size = this.read_target_usize(elem_size)?;
572 if let Some(size) = this.compute_size_in_bytes(Size::from_bytes(elem_size), items) {
573 let res = this.malloc(size.bytes(), AllocInit::Zero)?;
574 this.write_pointer(res, dest)?;
575 } else {
576 if this.target_os_is_unix() {
578 this.set_last_error(LibcError("ENOMEM"))?;
579 }
580 this.write_null(dest)?;
581 }
582 }
583 "free" => {
584 let [ptr] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
585 let ptr = this.read_pointer(ptr)?;
586 this.free(ptr)?;
587 }
588 "realloc" => {
589 let [old_ptr, new_size] =
590 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
591 let old_ptr = this.read_pointer(old_ptr)?;
592 let new_size = this.read_target_usize(new_size)?;
593 if new_size <= this.max_size_of_val().bytes() {
594 let res = this.realloc(old_ptr, new_size)?;
595 this.write_pointer(res, dest)?;
596 } else {
597 if this.target_os_is_unix() {
599 this.set_last_error(LibcError("ENOMEM"))?;
600 }
601 this.write_null(dest)?;
602 }
603 }
604
605 "memcmp" => {
607 let [left, right, n] =
608 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
609 let left = this.read_pointer(left)?;
610 let right = this.read_pointer(right)?;
611 let n = Size::from_bytes(this.read_target_usize(n)?);
612
613 this.ptr_get_alloc_id(left, 0)?;
615 this.ptr_get_alloc_id(right, 0)?;
616
617 let result = {
618 let left_bytes = this.read_bytes_ptr_strip_provenance(left, n)?;
619 let right_bytes = this.read_bytes_ptr_strip_provenance(right, n)?;
620
621 use std::cmp::Ordering::*;
622 match left_bytes.cmp(right_bytes) {
623 Less => -1i32,
624 Equal => 0,
625 Greater => 1,
626 }
627 };
628
629 this.write_scalar(Scalar::from_i32(result), dest)?;
630 }
631 "memrchr" => {
632 let [ptr, val, num] =
633 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
634 let ptr = this.read_pointer(ptr)?;
635 let val = this.read_scalar(val)?.to_i32()?;
636 let num = this.read_target_usize(num)?;
637 #[expect(clippy::as_conversions)]
639 let val = val as u8;
640
641 this.ptr_get_alloc_id(ptr, 0)?;
643
644 if let Some(idx) = this
645 .read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(num))?
646 .iter()
647 .rev()
648 .position(|&c| c == val)
649 {
650 let idx = u64::try_from(idx).unwrap();
651 #[expect(clippy::arithmetic_side_effects)] let new_ptr = ptr.wrapping_offset(Size::from_bytes(num - idx - 1), this);
653 this.write_pointer(new_ptr, dest)?;
654 } else {
655 this.write_null(dest)?;
656 }
657 }
658 "memchr" => {
659 let [ptr, val, num] =
660 this.check_shim_sig_lenient(abi, CanonAbi::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 #[expect(clippy::as_conversions)]
666 let val = val as u8;
667
668 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), 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_sig_lenient(abi, CanonAbi::C, link_name, args)?;
684 let ptr = this.read_pointer(ptr)?;
685 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_sig_lenient(abi, CanonAbi::C, link_name, args)?;
694 let ptr = this.read_pointer(ptr)?;
695 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] =
704 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
705 let ptr_dest = this.read_pointer(ptr_dest)?;
706 let ptr_src = this.read_pointer(ptr_src)?;
707 let n = this.read_target_usize(n)?;
708
709 this.ptr_get_alloc_id(ptr_dest, 0)?;
712 this.ptr_get_alloc_id(ptr_src, 0)?;
713
714 this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
715 this.write_pointer(ptr_dest, dest)?;
716 }
717 "strcpy" => {
718 let [ptr_dest, ptr_src] =
719 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
720 let ptr_dest = this.read_pointer(ptr_dest)?;
721 let ptr_src = this.read_pointer(ptr_src)?;
722
723 let n = this.read_c_str(ptr_src)?.len().strict_add(1);
730 this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
731 this.write_pointer(ptr_dest, dest)?;
732 }
733 "memset" => {
734 let [ptr_dest, val, n] =
735 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
736 let ptr_dest = this.read_pointer(ptr_dest)?;
737 let val = this.read_scalar(val)?.to_i32()?;
738 let n = this.read_target_usize(n)?;
739 #[expect(clippy::as_conversions)]
741 let val = val as u8;
742
743 this.ptr_get_alloc_id(ptr_dest, 0)?;
745
746 let bytes = std::iter::repeat_n(val, n.try_into().unwrap());
747 this.write_bytes_ptr(ptr_dest, bytes)?;
748 this.write_pointer(ptr_dest, dest)?;
749 }
750
751 "llvm.prefetch" => {
753 let [p, rw, loc, ty] =
754 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
755
756 let _ = this.read_pointer(p)?;
757 let rw = this.read_scalar(rw)?.to_i32()?;
758 let loc = this.read_scalar(loc)?.to_i32()?;
759 let ty = this.read_scalar(ty)?.to_i32()?;
760
761 if ty == 1 {
762 if !matches!(rw, 0 | 1) {
766 throw_unsup_format!("invalid `rw` value passed to `llvm.prefetch`: {}", rw);
767 }
768 if !matches!(loc, 0..=3) {
769 throw_unsup_format!(
770 "invalid `loc` value passed to `llvm.prefetch`: {}",
771 loc
772 );
773 }
774 } else {
775 throw_unsup_format!("unsupported `llvm.prefetch` type argument: {}", ty);
776 }
777 }
778 name if name.starts_with("llvm.ctpop.v") => {
781 let [op] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
782
783 let (op, op_len) = this.project_to_simd(op)?;
784 let (dest, dest_len) = this.project_to_simd(dest)?;
785
786 assert_eq!(dest_len, op_len);
787
788 for i in 0..dest_len {
789 let op = this.read_immediate(&this.project_index(&op, i)?)?;
790 let res = op.to_scalar().to_uint(op.layout.size)?.count_ones();
793
794 this.write_scalar(
795 Scalar::from_uint(res, op.layout.size),
796 &this.project_index(&dest, i)?,
797 )?;
798 }
799 }
800
801 name if name.starts_with("llvm.x86.")
803 && matches!(this.tcx.sess.target.arch, Arch::X86 | Arch::X86_64) =>
804 {
805 return shims::x86::EvalContextExt::emulate_x86_intrinsic(
806 this, link_name, abi, args, dest,
807 );
808 }
809 name if name.starts_with("llvm.aarch64.")
810 && this.tcx.sess.target.arch == Arch::AArch64 =>
811 {
812 return shims::aarch64::EvalContextExt::emulate_aarch64_intrinsic(
813 this, link_name, abi, args, dest,
814 );
815 }
816 "llvm.arm.hint" if this.tcx.sess.target.arch == Arch::Arm => {
818 let [arg] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
819 let arg = this.read_scalar(arg)?.to_i32()?;
820 match arg {
822 1 => {
824 this.expect_target_feature_for_intrinsic(link_name, "v6")?;
825 this.yield_active_thread();
826 }
827 _ => {
828 throw_unsup_format!("unsupported llvm.arm.hint argument {}", arg);
829 }
830 }
831 }
832
833 _ => {
835 #[expect(irrefutable_let_patterns)]
837 if let res = shims::math::EvalContextExt::emulate_foreign_item_inner(
838 this, link_name, abi, args, dest,
839 )? && !matches!(res, EmulateItemResult::NotSupported)
840 {
841 return interp_ok(res);
842 }
843
844 return match &this.tcx.sess.target.os {
846 _ if this.target_os_is_unix() =>
847 shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_inner(
848 this, link_name, abi, args, dest,
849 ),
850 Os::Windows =>
851 shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_inner(
852 this, link_name, abi, args, dest,
853 ),
854 _ => interp_ok(EmulateItemResult::NotSupported),
855 };
856 }
857 };
858 interp_ok(EmulateItemResult::NeedsReturn)
861 }
862}