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;
28 /// Returned by `emulate_foreign_item_by_name`.
29 pub enum EmulateByNameResult<'mir, 'tcx> {
30 /// The caller is expected to jump to the return block.
32 /// Jumping has already been taken care of.
34 /// A MIR body has been found for the function
35 MirBody(&'mir mir::Body<'tcx>, ty::Instance<'tcx>),
36 /// The item is not supported.
40 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
41 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
42 /// Returns the minimum alignment for the target architecture for allocations of the given size.
43 fn min_align(&self, size: u64, kind: MiriMemoryKind) -> Align {
44 let this = self.eval_context_ref();
45 // List taken from `libstd/sys_common/alloc.rs`.
46 let min_align = match this.tcx.sess.target.arch.as_ref() {
47 "x86" | "arm" | "mips" | "powerpc" | "powerpc64" | "asmjs" | "wasm32" => 8,
48 "x86_64" | "aarch64" | "mips64" | "s390x" | "sparc64" => 16,
49 arch => bug!("Unsupported target architecture: {}", arch),
51 // Windows always aligns, even small allocations.
52 // Source: <https://support.microsoft.com/en-us/help/286470/how-to-use-pageheap-exe-in-windows-xp-windows-2000-and-windows-server>
53 // But jemalloc does not, so for the C heap we only align if the allocation is sufficiently big.
54 if kind == MiriMemoryKind::WinHeap || size >= min_align {
55 return Align::from_bytes(min_align).unwrap();
57 // We have `size < min_align`. Round `size` *down* to the next power of two and use that.
58 fn prev_power_of_two(x: u64) -> u64 {
59 let next_pow2 = x.next_power_of_two();
61 // x *is* a power of two, just use that.
64 // x is between two powers, so next = 2*prev.
68 Align::from_bytes(prev_power_of_two(size)).unwrap()
76 ) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
77 let this = self.eval_context_mut();
81 let align = this.min_align(size, kind);
82 let ptr = this.allocate_ptr(Size::from_bytes(size), align, kind.into())?;
84 // We just allocated this, the access is definitely in-bounds.
85 this.write_bytes_ptr(ptr.into(), iter::repeat(0u8).take(size as usize)).unwrap();
91 fn free(&mut self, ptr: Pointer<Option<Tag>>, kind: MiriMemoryKind) -> InterpResult<'tcx> {
92 let this = self.eval_context_mut();
93 if !this.ptr_is_null(ptr)? {
94 this.deallocate_ptr(ptr, None, kind.into())?;
101 old_ptr: Pointer<Option<Tag>>,
103 kind: MiriMemoryKind,
104 ) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
105 let this = self.eval_context_mut();
106 let new_align = this.min_align(new_size, kind);
107 if this.ptr_is_null(old_ptr)? {
112 this.allocate_ptr(Size::from_bytes(new_size), new_align, kind.into())?;
117 this.deallocate_ptr(old_ptr, None, kind.into())?;
120 let new_ptr = this.reallocate_ptr(
123 Size::from_bytes(new_size),
132 /// Lookup the body of a function that has `link_name` as the symbol name.
133 fn lookup_exported_symbol(
136 ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
137 let this = self.eval_context_mut();
138 let tcx = this.tcx.tcx;
140 // If the result was cached, just return it.
141 // (Cannot use `or_insert` since the code below might have to throw an error.)
142 let entry = this.machine.exported_symbols_cache.entry(link_name);
143 let instance = *match entry {
144 Entry::Occupied(e) => e.into_mut(),
145 Entry::Vacant(e) => {
146 // Find it if it was not cached.
147 let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
148 // `dependency_formats` includes all the transitive informations needed to link a crate,
149 // which is what we need here since we need to dig out `exported_symbols` from all transitive
151 let dependency_formats = tcx.dependency_formats(());
152 let dependency_format = dependency_formats
154 .find(|(crate_type, _)| *crate_type == CrateType::Executable)
155 .expect("interpreting a non-executable crate");
156 for cnum in iter::once(LOCAL_CRATE).chain(
157 dependency_format.1.iter().enumerate().filter_map(|(num, &linkage)| {
158 (linkage != Linkage::NotLinked).then_some(CrateNum::new(num + 1))
161 // We can ignore `_export_info` here: we are a Rust crate, and everything is exported
162 // from a Rust crate.
163 for &(symbol, _export_info) in tcx.exported_symbols(cnum) {
164 if let ExportedSymbol::NonGeneric(def_id) = symbol {
165 let attrs = tcx.codegen_fn_attrs(def_id);
166 let symbol_name = if let Some(export_name) = attrs.export_name {
168 } else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
169 tcx.item_name(def_id)
171 // Skip over items without an explicitly defined symbol name.
174 if symbol_name == link_name {
175 if let Some((original_instance, original_cnum)) = instance_and_crate
177 // Make sure we are consistent wrt what is 'first' and 'second'.
179 tcx.def_span(original_instance.def_id()).data();
180 let span = tcx.def_span(def_id).data();
181 if original_span < span {
183 TerminationInfo::MultipleSymbolDefinitions {
185 first: original_span,
186 first_crate: tcx.crate_name(original_cnum),
188 second_crate: tcx.crate_name(cnum),
193 TerminationInfo::MultipleSymbolDefinitions {
196 first_crate: tcx.crate_name(cnum),
197 second: original_span,
198 second_crate: tcx.crate_name(original_cnum),
203 if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
205 "attempt to call an exported symbol that is not defined as a function"
208 instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
214 e.insert(instance_and_crate.map(|ic| ic.0))
218 None => Ok(None), // no symbol with this name
219 Some(instance) => Ok(Some((this.load_mir(instance.def, None)?, instance))),
223 /// Emulates calling a foreign item, failing if the item is not supported.
224 /// This function will handle `goto_block` if needed.
225 /// Returns Ok(None) if the foreign item was completely handled
226 /// by this function.
227 /// Returns Ok(Some(body)) if processing the foreign item
228 /// is delegated to another function.
229 fn emulate_foreign_item(
233 args: &[OpTy<'tcx, Tag>],
234 ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
235 unwind: StackPopUnwind,
236 ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
237 let this = self.eval_context_mut();
238 let link_name = this.item_link_name(def_id);
239 let tcx = this.tcx.tcx;
241 // First: functions that diverge.
242 let (dest, ret) = match ret {
244 match &*link_name.as_str() {
245 "miri_start_panic" => {
246 // `check_shim` happens inside `handle_miri_start_panic`.
247 this.handle_miri_start_panic(abi, link_name, args, unwind)?;
250 // This matches calls to the foreign item `panic_impl`.
251 // The implementation is provided by the function with the `#[panic_handler]` attribute.
253 // We don't use `check_shim` here because we are just forwarding to the lang
254 // item. Argument count checking will be performed when the returned `Body` is
256 this.check_abi_and_shim_symbol_clash(abi, Abi::Rust, link_name)?;
257 let panic_impl_id = tcx.lang_items().panic_impl().unwrap();
258 let panic_impl_instance = ty::Instance::mono(tcx, panic_impl_id);
260 &*this.load_mir(panic_impl_instance.def, None)?,
268 let exp_abi = if link_name.as_str() == "exit" {
269 Abi::C { unwind: false }
271 Abi::System { unwind: false }
273 let [code] = this.check_shim(abi, exp_abi, link_name, args)?;
274 // it's really u32 for ExitProcess, but we have to put it into the `Exit` variant anyway
275 let code = this.read_scalar(code)?.to_i32()?;
276 throw_machine_stop!(TerminationInfo::Exit(code.into()));
279 let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
280 throw_machine_stop!(TerminationInfo::Abort(
281 "the program aborted execution".to_owned()
285 if let Some(body) = this.lookup_exported_symbol(link_name)? {
286 return Ok(Some(body));
288 this.handle_unsupported(format!(
289 "can't call (diverging) foreign function: {}",
298 // Second: functions that return.
299 match this.emulate_foreign_item_by_name(link_name, abi, args, dest, ret)? {
300 EmulateByNameResult::NeedsJumping => {
301 trace!("{:?}", this.dump_place(**dest));
302 this.go_to_block(ret);
304 EmulateByNameResult::AlreadyJumped => (),
305 EmulateByNameResult::MirBody(mir, instance) => return Ok(Some((mir, instance))),
306 EmulateByNameResult::NotSupported => {
307 if let Some(body) = this.lookup_exported_symbol(link_name)? {
308 return Ok(Some(body));
311 this.handle_unsupported(format!("can't call foreign function: {}", link_name))?;
319 /// Emulates calling the internal __rust_* allocator functions
320 fn emulate_allocator(
323 default: impl FnOnce(&mut MiriEvalContext<'mir, 'tcx>) -> InterpResult<'tcx>,
324 ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
325 let this = self.eval_context_mut();
327 let allocator_kind = if let Some(allocator_kind) = this.tcx.allocator_kind(()) {
330 // in real code, this symbol does not exist without an allocator
331 return Ok(EmulateByNameResult::NotSupported);
334 match allocator_kind {
335 AllocatorKind::Global => {
336 let (body, instance) = this
337 .lookup_exported_symbol(symbol)?
338 .expect("symbol should be present if there is a global allocator");
340 Ok(EmulateByNameResult::MirBody(body, instance))
342 AllocatorKind::Default => {
344 Ok(EmulateByNameResult::NeedsJumping)
349 /// Emulates calling a foreign item using its name.
350 fn emulate_foreign_item_by_name(
354 args: &[OpTy<'tcx, Tag>],
355 dest: &PlaceTy<'tcx, Tag>,
356 ret: mir::BasicBlock,
357 ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
358 let this = self.eval_context_mut();
360 // Here we dispatch all the shims for foreign functions. If you have a platform specific
361 // shim, add it to the corresponding submodule.
362 match &*link_name.as_str() {
363 // Miri-specific extern functions
364 "miri_static_root" => {
365 let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
366 let ptr = this.read_pointer(ptr)?;
367 let (alloc_id, offset, _) = this.ptr_get_alloc_id(ptr)?;
368 if offset != Size::ZERO {
369 throw_unsup_format!("pointer passed to miri_static_root must point to beginning of an allocated block");
371 this.machine.static_roots.push(alloc_id);
374 // Obtains the size of a Miri backtrace. See the README for details.
375 "miri_backtrace_size" => {
376 this.handle_miri_backtrace_size(abi, link_name, args, dest)?;
379 // Obtains a Miri backtrace. See the README for details.
380 "miri_get_backtrace" => {
381 // `check_shim` happens inside `handle_miri_get_backtrace`.
382 this.handle_miri_get_backtrace(abi, link_name, args, dest)?;
385 // Resolves a Miri backtrace frame. See the README for details.
386 "miri_resolve_frame" => {
387 // `check_shim` happens inside `handle_miri_resolve_frame`.
388 this.handle_miri_resolve_frame(abi, link_name, args, dest)?;
391 // Writes the function and file names of a Miri backtrace frame into a user provided buffer. See the README for details.
392 "miri_resolve_frame_names" => {
393 this.handle_miri_resolve_frame_names(abi, link_name, args)?;
396 // Standard C allocation
398 let [size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
399 let size = this.read_scalar(size)?.to_machine_usize(this)?;
400 let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C)?;
401 this.write_pointer(res, dest)?;
404 let [items, len] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
405 let items = this.read_scalar(items)?.to_machine_usize(this)?;
406 let len = this.read_scalar(len)?.to_machine_usize(this)?;
408 items.checked_mul(len).ok_or_else(|| err_ub_format!("overflow during calloc size computation"))?;
409 let res = this.malloc(size, /*zero_init:*/ true, MiriMemoryKind::C)?;
410 this.write_pointer(res, dest)?;
413 let [ptr] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
414 let ptr = this.read_pointer(ptr)?;
415 this.free(ptr, MiriMemoryKind::C)?;
418 let [old_ptr, new_size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
419 let old_ptr = this.read_pointer(old_ptr)?;
420 let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
421 let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?;
422 this.write_pointer(res, dest)?;
427 let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
428 let size = this.read_scalar(size)?.to_machine_usize(this)?;
429 let align = this.read_scalar(align)?.to_machine_usize(this)?;
431 return this.emulate_allocator(Symbol::intern("__rg_alloc"), |this| {
432 Self::check_alloc_request(size, align)?;
434 let ptr = this.allocate_ptr(
435 Size::from_bytes(size),
436 Align::from_bytes(align).unwrap(),
437 MiriMemoryKind::Rust.into(),
440 this.write_pointer(ptr, dest)
443 "__rust_alloc_zeroed" => {
444 let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
445 let size = this.read_scalar(size)?.to_machine_usize(this)?;
446 let align = this.read_scalar(align)?.to_machine_usize(this)?;
448 return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| {
449 Self::check_alloc_request(size, align)?;
451 let ptr = this.allocate_ptr(
452 Size::from_bytes(size),
453 Align::from_bytes(align).unwrap(),
454 MiriMemoryKind::Rust.into(),
457 // We just allocated this, the access is definitely in-bounds.
458 this.write_bytes_ptr(ptr.into(), iter::repeat(0u8).take(usize::try_from(size).unwrap())).unwrap();
459 this.write_pointer(ptr, dest)
462 "__rust_dealloc" => {
463 let [ptr, old_size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
464 let ptr = this.read_pointer(ptr)?;
465 let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
466 let align = this.read_scalar(align)?.to_machine_usize(this)?;
468 return this.emulate_allocator(Symbol::intern("__rg_dealloc"), |this| {
469 // No need to check old_size/align; we anyway check that they match the allocation.
472 Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
473 MiriMemoryKind::Rust.into(),
477 "__rust_realloc" => {
478 let [ptr, old_size, align, new_size] = this.check_shim(abi, Abi::Rust, link_name, args)?;
479 let ptr = this.read_pointer(ptr)?;
480 let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
481 let align = this.read_scalar(align)?.to_machine_usize(this)?;
482 let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
483 // No need to check old_size; we anyway check that they match the allocation.
485 return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| {
486 Self::check_alloc_request(new_size, align)?;
488 let align = Align::from_bytes(align).unwrap();
489 let new_ptr = this.reallocate_ptr(
491 Some((Size::from_bytes(old_size), align)),
492 Size::from_bytes(new_size),
494 MiriMemoryKind::Rust.into(),
496 this.write_pointer(new_ptr, dest)
500 // C memory handling functions
502 let [left, right, n] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
503 let left = this.read_pointer(left)?;
504 let right = this.read_pointer(right)?;
505 let n = Size::from_bytes(this.read_scalar(n)?.to_machine_usize(this)?);
508 let left_bytes = this.read_bytes_ptr(left, n)?;
509 let right_bytes = this.read_bytes_ptr(right, n)?;
511 use std::cmp::Ordering::*;
512 match left_bytes.cmp(right_bytes) {
519 this.write_scalar(Scalar::from_i32(result), dest)?;
522 let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
523 let ptr = this.read_pointer(ptr)?;
524 let val = this.read_scalar(val)?.to_i32()? as u8;
525 let num = this.read_scalar(num)?.to_machine_usize(this)?;
526 if let Some(idx) = this
527 .read_bytes_ptr(ptr, Size::from_bytes(num))?
530 .position(|&c| c == val)
532 let new_ptr = ptr.offset(Size::from_bytes(num - idx as u64 - 1), this)?;
533 this.write_pointer(new_ptr, dest)?;
535 this.write_null(dest)?;
539 let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
540 let ptr = this.read_pointer(ptr)?;
541 let val = this.read_scalar(val)?.to_i32()? as u8;
542 let num = this.read_scalar(num)?.to_machine_usize(this)?;
544 .read_bytes_ptr(ptr, Size::from_bytes(num))?
546 .position(|&c| c == val);
547 if let Some(idx) = idx {
548 let new_ptr = ptr.offset(Size::from_bytes(idx as u64), this)?;
549 this.write_pointer(new_ptr, dest)?;
551 this.write_null(dest)?;
555 let [ptr] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
556 let ptr = this.read_pointer(ptr)?;
557 let n = this.read_c_str(ptr)?.len();
558 this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
571 let [f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
572 // FIXME: Using host floats.
573 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
574 let f = match &*link_name.as_str() {
584 this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
591 let [f1, f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
592 // underscore case for windows, here and below
593 // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
594 // FIXME: Using host floats.
595 let f1 = f32::from_bits(this.read_scalar(f1)?.to_u32()?);
596 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
597 let n = match &*link_name.as_str() {
598 "_hypotf" | "hypotf" => f1.hypot(f2),
599 "atan2f" => f1.atan2(f2),
602 this.write_scalar(Scalar::from_u32(n.to_bits()), dest)?;
613 let [f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
614 // FIXME: Using host floats.
615 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
616 let f = match &*link_name.as_str() {
626 this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
633 let [f1, f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
634 // FIXME: Using host floats.
635 let f1 = f64::from_bits(this.read_scalar(f1)?.to_u64()?);
636 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
637 let n = match &*link_name.as_str() {
638 "_hypot" | "hypot" => f1.hypot(f2),
639 "atan2" => f1.atan2(f2),
642 this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
649 let [x, exp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
650 // For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
651 let x = this.read_scalar(x)?.to_f64()?;
652 let exp = this.read_scalar(exp)?.to_i32()?;
654 // Saturating cast to i16. Even those are outside the valid exponent range to
655 // `scalbn` below will do its over/underflow handling.
656 let exp = if exp > i32::from(i16::MAX) {
658 } else if exp < i32::from(i16::MIN) {
661 exp.try_into().unwrap()
664 let res = x.scalbn(exp);
665 this.write_scalar(Scalar::from_f64(res), dest)?;
668 // Architecture-specific shims
669 "llvm.x86.addcarry.64" if this.tcx.sess.target.arch == "x86_64" => {
670 // Computes u8+u64+u64, returning tuple (u8,u64) comprising the output carry and truncated sum.
671 let [c_in, a, b] = this.check_shim(abi, Abi::Unadjusted, link_name, args)?;
672 let c_in = this.read_scalar(c_in)?.to_u8()?;
673 let a = this.read_scalar(a)?.to_u64()?;
674 let b = this.read_scalar(b)?.to_u64()?;
676 let wide_sum = u128::from(c_in) + u128::from(a) + u128::from(b);
677 let (c_out, sum) = ((wide_sum >> 64).truncate::<u8>(), wide_sum.truncate::<u64>());
679 let c_out_field = this.place_field(dest, 0)?;
680 this.write_scalar(Scalar::from_u8(c_out), &c_out_field)?;
681 let sum_field = this.place_field(dest, 1)?;
682 this.write_scalar(Scalar::from_u64(sum), &sum_field)?;
684 "llvm.x86.sse2.pause" if this.tcx.sess.target.arch == "x86" || this.tcx.sess.target.arch == "x86_64" => {
685 let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
686 this.yield_active_thread();
688 "llvm.aarch64.isb" if this.tcx.sess.target.arch == "aarch64" => {
689 let [arg] = this.check_shim(abi, Abi::Unadjusted, link_name, args)?;
690 let arg = this.read_scalar(arg)?.to_i32()?;
692 15 => { // SY ("full system scope")
693 this.yield_active_thread();
696 throw_unsup_format!("unsupported llvm.aarch64.isb argument {}", arg);
701 // Platform-specific shims
702 _ => match this.tcx.sess.target.os.as_ref() {
703 "linux" | "macos" => return shims::posix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
704 "windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
705 target => throw_unsup_format!("the target `{}` is not supported", target),
709 // We only fall through to here if we did *not* hit the `_` arm above,
710 // i.e., if we actually emulated the function.
711 Ok(EmulateByNameResult::NeedsJumping)
714 /// Check some basic requirements for this allocation request:
715 /// non-zero size, power-of-two alignment.
716 fn check_alloc_request(size: u64, align: u64) -> InterpResult<'tcx> {
718 throw_ub_format!("creating allocation with size 0");
720 if !align.is_power_of_two() {
721 throw_ub_format!("creating allocation with non-power-of-two alignment {}", align);