1 use std::{collections::hash_map::Entry, iter};
5 use rustc_apfloat::Float;
6 use rustc_ast::expand::allocator::AllocatorKind;
9 def_id::{CrateNum, DefId, LOCAL_CRATE},
11 use rustc_middle::middle::{
12 codegen_fn_attrs::CodegenFnAttrFlags, dependency_format::Linkage,
13 exported_symbols::ExportedSymbol,
15 use rustc_middle::mir;
17 use rustc_session::config::CrateType;
18 use rustc_span::Symbol;
24 use super::backtrace::EvalContextExt as _;
25 use crate::helpers::{convert::Truncate, target_os_is_unix};
27 use crate::shims::ffi_support::EvalContextExt as _;
30 /// Returned by `emulate_foreign_item_by_name`.
31 pub enum EmulateByNameResult<'mir, 'tcx> {
32 /// The caller is expected to jump to the return block.
34 /// Jumping has already been taken care of.
36 /// A MIR body has been found for the function.
37 MirBody(&'mir mir::Body<'tcx>, ty::Instance<'tcx>),
38 /// The item is not supported.
42 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
43 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
44 /// Returns the minimum alignment for the target architecture for allocations of the given size.
45 fn min_align(&self, size: u64, kind: MiriMemoryKind) -> Align {
46 let this = self.eval_context_ref();
47 // List taken from `library/std/src/sys/common/alloc.rs`.
48 // This list should be kept in sync with the one from libstd.
49 let min_align = match this.tcx.sess.target.arch.as_ref() {
50 "x86" | "arm" | "mips" | "powerpc" | "powerpc64" | "asmjs" | "wasm32" => 8,
51 "x86_64" | "aarch64" | "mips64" | "s390x" | "sparc64" => 16,
52 arch => bug!("Unsupported target architecture: {}", arch),
54 // Windows always aligns, even small allocations.
55 // Source: <https://support.microsoft.com/en-us/help/286470/how-to-use-pageheap-exe-in-windows-xp-windows-2000-and-windows-server>
56 // But jemalloc does not, so for the C heap we only align if the allocation is sufficiently big.
57 if kind == MiriMemoryKind::WinHeap || size >= min_align {
58 return Align::from_bytes(min_align).unwrap();
60 // We have `size < min_align`. Round `size` *down* to the next power of two and use that.
61 fn prev_power_of_two(x: u64) -> u64 {
62 let next_pow2 = x.next_power_of_two();
64 // x *is* a power of two, just use that.
67 // x is between two powers, so next = 2*prev.
71 Align::from_bytes(prev_power_of_two(size)).unwrap()
79 ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
80 let this = self.eval_context_mut();
84 let align = this.min_align(size, kind);
85 let ptr = this.allocate_ptr(Size::from_bytes(size), align, kind.into())?;
87 // We just allocated this, the access is definitely in-bounds and fits into our address space.
90 iter::repeat(0u8).take(usize::try_from(size).unwrap()),
100 ptr: Pointer<Option<Provenance>>,
101 kind: MiriMemoryKind,
102 ) -> InterpResult<'tcx> {
103 let this = self.eval_context_mut();
104 if !this.ptr_is_null(ptr)? {
105 this.deallocate_ptr(ptr, None, kind.into())?;
112 old_ptr: Pointer<Option<Provenance>>,
114 kind: MiriMemoryKind,
115 ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
116 let this = self.eval_context_mut();
117 let new_align = this.min_align(new_size, kind);
118 if this.ptr_is_null(old_ptr)? {
123 this.allocate_ptr(Size::from_bytes(new_size), new_align, kind.into())?;
128 this.deallocate_ptr(old_ptr, None, kind.into())?;
131 let new_ptr = this.reallocate_ptr(
134 Size::from_bytes(new_size),
143 /// Lookup the body of a function that has `link_name` as the symbol name.
144 fn lookup_exported_symbol(
147 ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
148 let this = self.eval_context_mut();
149 let tcx = this.tcx.tcx;
151 // If the result was cached, just return it.
152 // (Cannot use `or_insert` since the code below might have to throw an error.)
153 let entry = this.machine.exported_symbols_cache.entry(link_name);
154 let instance = *match entry {
155 Entry::Occupied(e) => e.into_mut(),
156 Entry::Vacant(e) => {
157 // Find it if it was not cached.
158 let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
159 // `dependency_formats` includes all the transitive informations needed to link a crate,
160 // which is what we need here since we need to dig out `exported_symbols` from all transitive
162 let dependency_formats = tcx.dependency_formats(());
163 let dependency_format = dependency_formats
165 .find(|(crate_type, _)| *crate_type == CrateType::Executable)
166 .expect("interpreting a non-executable crate");
167 for cnum in iter::once(LOCAL_CRATE).chain(
168 dependency_format.1.iter().enumerate().filter_map(|(num, &linkage)| {
169 // We add 1 to the number because that's what rustc also does everywhere it
170 // calls `CrateNum::new`...
171 #[allow(clippy::integer_arithmetic)]
172 (linkage != Linkage::NotLinked).then_some(CrateNum::new(num + 1))
175 // We can ignore `_export_info` here: we are a Rust crate, and everything is exported
176 // from a Rust crate.
177 for &(symbol, _export_info) in tcx.exported_symbols(cnum) {
178 if let ExportedSymbol::NonGeneric(def_id) = symbol {
179 let attrs = tcx.codegen_fn_attrs(def_id);
180 let symbol_name = if let Some(export_name) = attrs.export_name {
182 } else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
183 tcx.item_name(def_id)
185 // Skip over items without an explicitly defined symbol name.
188 if symbol_name == link_name {
189 if let Some((original_instance, original_cnum)) = instance_and_crate
191 // Make sure we are consistent wrt what is 'first' and 'second'.
193 tcx.def_span(original_instance.def_id()).data();
194 let span = tcx.def_span(def_id).data();
195 if original_span < span {
197 TerminationInfo::MultipleSymbolDefinitions {
199 first: original_span,
200 first_crate: tcx.crate_name(original_cnum),
202 second_crate: tcx.crate_name(cnum),
207 TerminationInfo::MultipleSymbolDefinitions {
210 first_crate: tcx.crate_name(cnum),
211 second: original_span,
212 second_crate: tcx.crate_name(original_cnum),
217 if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
219 "attempt to call an exported symbol that is not defined as a function"
222 instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
228 e.insert(instance_and_crate.map(|ic| ic.0))
232 None => Ok(None), // no symbol with this name
233 Some(instance) => Ok(Some((this.load_mir(instance.def, None)?, instance))),
237 /// Emulates calling a foreign item, failing if the item is not supported.
238 /// This function will handle `goto_block` if needed.
239 /// Returns Ok(None) if the foreign item was completely handled
240 /// by this function.
241 /// Returns Ok(Some(body)) if processing the foreign item
242 /// is delegated to another function.
243 fn emulate_foreign_item(
247 args: &[OpTy<'tcx, Provenance>],
248 dest: &PlaceTy<'tcx, Provenance>,
249 ret: Option<mir::BasicBlock>,
250 unwind: StackPopUnwind,
251 ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
252 let this = self.eval_context_mut();
253 let link_name = this.item_link_name(def_id);
254 let tcx = this.tcx.tcx;
256 // First: functions that diverge.
257 let ret = match ret {
259 match link_name.as_str() {
260 "miri_start_panic" => {
261 // `check_shim` happens inside `handle_miri_start_panic`.
262 this.handle_miri_start_panic(abi, link_name, args, unwind)?;
265 // This matches calls to the foreign item `panic_impl`.
266 // The implementation is provided by the function with the `#[panic_handler]` attribute.
268 // We don't use `check_shim` here because we are just forwarding to the lang
269 // item. Argument count checking will be performed when the returned `Body` is
271 this.check_abi_and_shim_symbol_clash(abi, Abi::Rust, link_name)?;
272 let panic_impl_id = tcx.lang_items().panic_impl().unwrap();
273 let panic_impl_instance = ty::Instance::mono(tcx, panic_impl_id);
275 this.load_mir(panic_impl_instance.def, None)?,
283 let exp_abi = if link_name.as_str() == "exit" {
284 Abi::C { unwind: false }
286 Abi::System { unwind: false }
288 let [code] = this.check_shim(abi, exp_abi, link_name, args)?;
289 // it's really u32 for ExitProcess, but we have to put it into the `Exit` variant anyway
290 let code = this.read_scalar(code)?.to_i32()?;
291 throw_machine_stop!(TerminationInfo::Exit(code.into()));
294 let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
295 throw_machine_stop!(TerminationInfo::Abort(
296 "the program aborted execution".to_owned()
300 if let Some(body) = this.lookup_exported_symbol(link_name)? {
301 return Ok(Some(body));
303 this.handle_unsupported(format!(
304 "can't call (diverging) foreign function: {}",
313 // Second: functions that return immediately.
314 match this.emulate_foreign_item_by_name(link_name, abi, args, dest)? {
315 EmulateByNameResult::NeedsJumping => {
316 trace!("{:?}", this.dump_place(**dest));
317 this.go_to_block(ret);
319 EmulateByNameResult::AlreadyJumped => (),
320 EmulateByNameResult::MirBody(mir, instance) => return Ok(Some((mir, instance))),
321 EmulateByNameResult::NotSupported => {
322 if let Some(body) = this.lookup_exported_symbol(link_name)? {
323 return Ok(Some(body));
326 this.handle_unsupported(format!("can't call foreign function: {}", link_name))?;
334 /// Emulates calling the internal __rust_* allocator functions
335 fn emulate_allocator(
338 default: impl FnOnce(&mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx>,
339 ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
340 let this = self.eval_context_mut();
342 let allocator_kind = if let Some(allocator_kind) = this.tcx.allocator_kind(()) {
345 // in real code, this symbol does not exist without an allocator
346 return Ok(EmulateByNameResult::NotSupported);
349 match allocator_kind {
350 AllocatorKind::Global => {
351 let (body, instance) = this
352 .lookup_exported_symbol(symbol)?
353 .expect("symbol should be present if there is a global allocator");
355 Ok(EmulateByNameResult::MirBody(body, instance))
357 AllocatorKind::Default => {
359 Ok(EmulateByNameResult::NeedsJumping)
364 /// Emulates calling a foreign item using its name.
365 fn emulate_foreign_item_by_name(
369 args: &[OpTy<'tcx, Provenance>],
370 dest: &PlaceTy<'tcx, Provenance>,
371 ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
372 let this = self.eval_context_mut();
374 // First deal with any external C functions in linked .so file.
376 if this.machine.external_so_lib.as_ref().is_some() {
377 // An Ok(false) here means that the function being called was not exported
378 // by the specified `.so` file; we should continue and check if it corresponds to
380 if this.call_external_c_fct(link_name, dest, args)? {
381 return Ok(EmulateByNameResult::NeedsJumping);
385 // When adding a new shim, you should follow the following pattern:
388 // let [arg1, arg2, arg3] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
389 // let result = this.shim_name(arg1, arg2, arg3)?;
390 // this.write_scalar(result, dest)?;
393 // and then define `shim_name` as a helper function in an extension trait in a suitable file
394 // (see e.g. `unix/fs.rs`):
398 // arg1: &OpTy<'tcx, Provenance>,
399 // arg2: &OpTy<'tcx, Provenance>,
400 // arg3: &OpTy<'tcx, Provenance>)
401 // -> InterpResult<'tcx, Scalar<Provenance>> {
402 // let this = self.eval_context_mut();
404 // // First thing: load all the arguments. Details depend on the shim.
405 // let arg1 = this.read_scalar(arg1)?.to_u32()?;
406 // let arg2 = this.read_pointer(arg2)?; // when you need to work with the pointer directly
407 // let arg3 = this.deref_operand(arg3)?; // when you want to load/store through the pointer at its declared type
411 // Ok(Scalar::from_u32(42))
414 // You might find existing shims not following this pattern, most
415 // likely because they predate it or because for some reason they cannot be made to fit.
417 // Here we dispatch all the shims for foreign functions. If you have a platform specific
418 // shim, add it to the corresponding submodule.
419 match link_name.as_str() {
420 // Miri-specific extern functions
421 "miri_static_root" => {
422 let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
423 let ptr = this.read_pointer(ptr)?;
424 let (alloc_id, offset, _) = this.ptr_get_alloc_id(ptr)?;
425 if offset != Size::ZERO {
426 throw_unsup_format!("pointer passed to miri_static_root must point to beginning of an allocated block");
428 this.machine.static_roots.push(alloc_id);
431 // Obtains the size of a Miri backtrace. See the README for details.
432 "miri_backtrace_size" => {
433 this.handle_miri_backtrace_size(abi, link_name, args, dest)?;
436 // Obtains a Miri backtrace. See the README for details.
437 "miri_get_backtrace" => {
438 // `check_shim` happens inside `handle_miri_get_backtrace`.
439 this.handle_miri_get_backtrace(abi, link_name, args, dest)?;
442 // Resolves a Miri backtrace frame. See the README for details.
443 "miri_resolve_frame" => {
444 // `check_shim` happens inside `handle_miri_resolve_frame`.
445 this.handle_miri_resolve_frame(abi, link_name, args, dest)?;
448 // Writes the function and file names of a Miri backtrace frame into a user provided buffer. See the README for details.
449 "miri_resolve_frame_names" => {
450 this.handle_miri_resolve_frame_names(abi, link_name, args)?;
453 // Standard C allocation
455 let [size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
456 let size = this.read_scalar(size)?.to_machine_usize(this)?;
457 let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C)?;
458 this.write_pointer(res, dest)?;
461 let [items, len] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
462 let items = this.read_scalar(items)?.to_machine_usize(this)?;
463 let len = this.read_scalar(len)?.to_machine_usize(this)?;
465 items.checked_mul(len).ok_or_else(|| err_ub_format!("overflow during calloc size computation"))?;
466 let res = this.malloc(size, /*zero_init:*/ true, MiriMemoryKind::C)?;
467 this.write_pointer(res, dest)?;
470 let [ptr] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
471 let ptr = this.read_pointer(ptr)?;
472 this.free(ptr, MiriMemoryKind::C)?;
475 let [old_ptr, new_size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
476 let old_ptr = this.read_pointer(old_ptr)?;
477 let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
478 let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?;
479 this.write_pointer(res, dest)?;
484 let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
485 let size = this.read_scalar(size)?.to_machine_usize(this)?;
486 let align = this.read_scalar(align)?.to_machine_usize(this)?;
488 return this.emulate_allocator(Symbol::intern("__rg_alloc"), |this| {
489 Self::check_alloc_request(size, align)?;
491 let ptr = this.allocate_ptr(
492 Size::from_bytes(size),
493 Align::from_bytes(align).unwrap(),
494 MiriMemoryKind::Rust.into(),
497 this.write_pointer(ptr, dest)
500 "__rust_alloc_zeroed" => {
501 let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
502 let size = this.read_scalar(size)?.to_machine_usize(this)?;
503 let align = this.read_scalar(align)?.to_machine_usize(this)?;
505 return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| {
506 Self::check_alloc_request(size, align)?;
508 let ptr = this.allocate_ptr(
509 Size::from_bytes(size),
510 Align::from_bytes(align).unwrap(),
511 MiriMemoryKind::Rust.into(),
514 // We just allocated this, the access is definitely in-bounds.
515 this.write_bytes_ptr(ptr.into(), iter::repeat(0u8).take(usize::try_from(size).unwrap())).unwrap();
516 this.write_pointer(ptr, dest)
519 "__rust_dealloc" => {
520 let [ptr, old_size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
521 let ptr = this.read_pointer(ptr)?;
522 let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
523 let align = this.read_scalar(align)?.to_machine_usize(this)?;
525 return this.emulate_allocator(Symbol::intern("__rg_dealloc"), |this| {
526 // No need to check old_size/align; we anyway check that they match the allocation.
529 Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
530 MiriMemoryKind::Rust.into(),
534 "__rust_realloc" => {
535 let [ptr, old_size, align, new_size] = this.check_shim(abi, Abi::Rust, link_name, args)?;
536 let ptr = this.read_pointer(ptr)?;
537 let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
538 let align = this.read_scalar(align)?.to_machine_usize(this)?;
539 let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
540 // No need to check old_size; we anyway check that they match the allocation.
542 return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| {
543 Self::check_alloc_request(new_size, align)?;
545 let align = Align::from_bytes(align).unwrap();
546 let new_ptr = this.reallocate_ptr(
548 Some((Size::from_bytes(old_size), align)),
549 Size::from_bytes(new_size),
551 MiriMemoryKind::Rust.into(),
553 this.write_pointer(new_ptr, dest)
557 // C memory handling functions
559 let [left, right, n] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
560 let left = this.read_pointer(left)?;
561 let right = this.read_pointer(right)?;
562 let n = Size::from_bytes(this.read_scalar(n)?.to_machine_usize(this)?);
565 let left_bytes = this.read_bytes_ptr_strip_provenance(left, n)?;
566 let right_bytes = this.read_bytes_ptr_strip_provenance(right, n)?;
568 use std::cmp::Ordering::*;
569 match left_bytes.cmp(right_bytes) {
576 this.write_scalar(Scalar::from_i32(result), dest)?;
579 let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
580 let ptr = this.read_pointer(ptr)?;
581 let val = this.read_scalar(val)?.to_i32()?;
582 let num = this.read_scalar(num)?.to_machine_usize(this)?;
583 // The docs say val is "interpreted as unsigned char".
584 #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
587 if let Some(idx) = this
588 .read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(num))?
591 .position(|&c| c == val)
593 let idx = u64::try_from(idx).unwrap();
594 #[allow(clippy::integer_arithmetic)] // idx < num, so this never wraps
595 let new_ptr = ptr.offset(Size::from_bytes(num - idx - 1), this)?;
596 this.write_pointer(new_ptr, dest)?;
598 this.write_null(dest)?;
602 let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
603 let ptr = this.read_pointer(ptr)?;
604 let val = this.read_scalar(val)?.to_i32()?;
605 let num = this.read_scalar(num)?.to_machine_usize(this)?;
606 // The docs say val is "interpreted as unsigned char".
607 #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
611 .read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(num))?
613 .position(|&c| c == val);
614 if let Some(idx) = idx {
615 let new_ptr = ptr.offset(Size::from_bytes(idx as u64), this)?;
616 this.write_pointer(new_ptr, dest)?;
618 this.write_null(dest)?;
622 let [ptr] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
623 let ptr = this.read_pointer(ptr)?;
624 let n = this.read_c_str(ptr)?.len();
625 this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
628 // math functions (note that there are also intrinsics for some other functions)
641 let [f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
642 // FIXME: Using host floats.
643 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
644 let res = match link_name.as_str() {
653 "log1pf" => f.ln_1p(),
654 "expm1f" => f.exp_m1(),
657 this.write_scalar(Scalar::from_u32(res.to_bits()), dest)?;
665 let [f1, f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
666 // underscore case for windows, here and below
667 // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
668 // FIXME: Using host floats.
669 let f1 = f32::from_bits(this.read_scalar(f1)?.to_u32()?);
670 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
671 let res = match link_name.as_str() {
672 "_hypotf" | "hypotf" => f1.hypot(f2),
673 "atan2f" => f1.atan2(f2),
675 "fdimf" => f1.abs_sub(f2),
678 this.write_scalar(Scalar::from_u32(res.to_bits()), dest)?;
692 let [f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
693 // FIXME: Using host floats.
694 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
695 let res = match link_name.as_str() {
704 "log1p" => f.ln_1p(),
705 "expm1" => f.exp_m1(),
708 this.write_scalar(Scalar::from_u64(res.to_bits()), dest)?;
716 let [f1, f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
717 // FIXME: Using host floats.
718 let f1 = f64::from_bits(this.read_scalar(f1)?.to_u64()?);
719 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
720 let res = match link_name.as_str() {
721 "_hypot" | "hypot" => f1.hypot(f2),
722 "atan2" => f1.atan2(f2),
724 "fdim" => f1.abs_sub(f2),
727 this.write_scalar(Scalar::from_u64(res.to_bits()), dest)?;
734 let [x, exp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
735 // For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
736 let x = this.read_scalar(x)?.to_f64()?;
737 let exp = this.read_scalar(exp)?.to_i32()?;
739 // Saturating cast to i16. Even those are outside the valid exponent range so
740 // `scalbn` below will do its over/underflow handling.
741 let exp = if exp > i32::from(i16::MAX) {
743 } else if exp < i32::from(i16::MIN) {
746 exp.try_into().unwrap()
749 let res = x.scalbn(exp);
750 this.write_scalar(Scalar::from_f64(res), dest)?;
753 // Architecture-specific shims
754 "llvm.x86.addcarry.64" if this.tcx.sess.target.arch == "x86_64" => {
755 // Computes u8+u64+u64, returning tuple (u8,u64) comprising the output carry and truncated sum.
756 let [c_in, a, b] = this.check_shim(abi, Abi::Unadjusted, link_name, args)?;
757 let c_in = this.read_scalar(c_in)?.to_u8()?;
758 let a = this.read_scalar(a)?.to_u64()?;
759 let b = this.read_scalar(b)?.to_u64()?;
761 #[allow(clippy::integer_arithmetic)] // adding two u64 and a u8 cannot wrap in a u128
762 let wide_sum = u128::from(c_in) + u128::from(a) + u128::from(b);
763 #[allow(clippy::integer_arithmetic)] // it's a u128, we can shift by 64
764 let (c_out, sum) = ((wide_sum >> 64).truncate::<u8>(), wide_sum.truncate::<u64>());
766 let c_out_field = this.place_field(dest, 0)?;
767 this.write_scalar(Scalar::from_u8(c_out), &c_out_field)?;
768 let sum_field = this.place_field(dest, 1)?;
769 this.write_scalar(Scalar::from_u64(sum), &sum_field)?;
771 "llvm.x86.sse2.pause" if this.tcx.sess.target.arch == "x86" || this.tcx.sess.target.arch == "x86_64" => {
772 let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
773 this.yield_active_thread();
775 "llvm.aarch64.isb" if this.tcx.sess.target.arch == "aarch64" => {
776 let [arg] = this.check_shim(abi, Abi::Unadjusted, link_name, args)?;
777 let arg = this.read_scalar(arg)?.to_i32()?;
779 15 => { // SY ("full system scope")
780 this.yield_active_thread();
783 throw_unsup_format!("unsupported llvm.aarch64.isb argument {}", arg);
788 // Platform-specific shims
789 _ => match this.tcx.sess.target.os.as_ref() {
790 target if target_os_is_unix(target) => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest),
791 "windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest),
792 target => throw_unsup_format!("the target `{}` is not supported", target),
795 // We only fall through to here if we did *not* hit the `_` arm above,
796 // i.e., if we actually emulated the function with one of the shims.
797 Ok(EmulateByNameResult::NeedsJumping)
800 /// Check some basic requirements for this allocation request:
801 /// non-zero size, power-of-two alignment.
802 fn check_alloc_request(size: u64, align: u64) -> InterpResult<'tcx> {
804 throw_ub_format!("creating allocation with size 0");
806 if !align.is_power_of_two() {
807 throw_ub_format!("creating allocation with non-power-of-two alignment {}", align);