]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items.rs
rustup
[rust.git] / src / shims / foreign_items.rs
1 use std::{collections::hash_map::Entry, iter};
2
3 use log::trace;
4
5 use rustc_apfloat::Float;
6 use rustc_ast::expand::allocator::AllocatorKind;
7 use rustc_hir::{
8     def::DefKind,
9     def_id::{CrateNum, DefId, LOCAL_CRATE},
10 };
11 use rustc_middle::middle::{
12     codegen_fn_attrs::CodegenFnAttrFlags, dependency_format::Linkage,
13     exported_symbols::ExportedSymbol,
14 };
15 use rustc_middle::mir;
16 use rustc_middle::ty;
17 use rustc_session::config::CrateType;
18 use rustc_span::Symbol;
19 use rustc_target::{
20     abi::{Align, Size},
21     spec::abi::Abi,
22 };
23
24 use super::backtrace::EvalContextExt as _;
25 use crate::helpers::convert::Truncate;
26 use crate::*;
27
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.
31     NeedsJumping,
32     /// Jumping has already been taken care of.
33     AlreadyJumped,
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.
37     NotSupported,
38 }
39
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),
50         };
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();
56         }
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();
60             if next_pow2 == x {
61                 // x *is* a power of two, just use that.
62                 x
63             } else {
64                 // x is between two powers, so next = 2*prev.
65                 next_pow2 / 2
66             }
67         }
68         Align::from_bytes(prev_power_of_two(size)).unwrap()
69     }
70
71     fn malloc(
72         &mut self,
73         size: u64,
74         zero_init: bool,
75         kind: MiriMemoryKind,
76     ) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
77         let this = self.eval_context_mut();
78         if size == 0 {
79             Ok(Pointer::null())
80         } else {
81             let align = this.min_align(size, kind);
82             let ptr = this.allocate_ptr(Size::from_bytes(size), align, kind.into())?;
83             if zero_init {
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();
86             }
87             Ok(ptr.into())
88         }
89     }
90
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())?;
95         }
96         Ok(())
97     }
98
99     fn realloc(
100         &mut self,
101         old_ptr: Pointer<Option<Tag>>,
102         new_size: u64,
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)? {
108             if new_size == 0 {
109                 Ok(Pointer::null())
110             } else {
111                 let new_ptr =
112                     this.allocate_ptr(Size::from_bytes(new_size), new_align, kind.into())?;
113                 Ok(new_ptr.into())
114             }
115         } else {
116             if new_size == 0 {
117                 this.deallocate_ptr(old_ptr, None, kind.into())?;
118                 Ok(Pointer::null())
119             } else {
120                 let new_ptr = this.reallocate_ptr(
121                     old_ptr,
122                     None,
123                     Size::from_bytes(new_size),
124                     new_align,
125                     kind.into(),
126                 )?;
127                 Ok(new_ptr.into())
128             }
129         }
130     }
131
132     /// Lookup the body of a function that has `link_name` as the symbol name.
133     fn lookup_exported_symbol(
134         &mut self,
135         link_name: 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;
139
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
150                 // dependencies.
151                 let dependency_formats = tcx.dependency_formats(());
152                 let dependency_format = dependency_formats
153                     .iter()
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))
159                     }),
160                 ) {
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 {
167                                 export_name
168                             } else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
169                                 tcx.item_name(def_id)
170                             } else {
171                                 // Skip over items without an explicitly defined symbol name.
172                                 continue;
173                             };
174                             if symbol_name == link_name {
175                                 if let Some((original_instance, original_cnum)) = instance_and_crate
176                                 {
177                                     // Make sure we are consistent wrt what is 'first' and 'second'.
178                                     let original_span =
179                                         tcx.def_span(original_instance.def_id()).data();
180                                     let span = tcx.def_span(def_id).data();
181                                     if original_span < span {
182                                         throw_machine_stop!(
183                                             TerminationInfo::MultipleSymbolDefinitions {
184                                                 link_name,
185                                                 first: original_span,
186                                                 first_crate: tcx.crate_name(original_cnum),
187                                                 second: span,
188                                                 second_crate: tcx.crate_name(cnum),
189                                             }
190                                         );
191                                     } else {
192                                         throw_machine_stop!(
193                                             TerminationInfo::MultipleSymbolDefinitions {
194                                                 link_name,
195                                                 first: span,
196                                                 first_crate: tcx.crate_name(cnum),
197                                                 second: original_span,
198                                                 second_crate: tcx.crate_name(original_cnum),
199                                             }
200                                         );
201                                     }
202                                 }
203                                 if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
204                                     throw_ub_format!(
205                                         "attempt to call an exported symbol that is not defined as a function"
206                                     );
207                                 }
208                                 instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
209                             }
210                         }
211                     }
212                 }
213
214                 e.insert(instance_and_crate.map(|ic| ic.0))
215             }
216         };
217         match instance {
218             None => Ok(None), // no symbol with this name
219             Some(instance) => Ok(Some((this.load_mir(instance.def, None)?, instance))),
220         }
221     }
222
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(
230         &mut self,
231         def_id: DefId,
232         abi: Abi,
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;
240
241         // First: functions that diverge.
242         let (dest, ret) = match ret {
243             None =>
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)?;
248                         return Ok(None);
249                     }
250                     // This matches calls to the foreign item `panic_impl`.
251                     // The implementation is provided by the function with the `#[panic_handler]` attribute.
252                     "panic_impl" => {
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
255                         // called.
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);
259                         return Ok(Some((
260                             &*this.load_mir(panic_impl_instance.def, None)?,
261                             panic_impl_instance,
262                         )));
263                     }
264                     #[rustfmt::skip]
265                     | "exit"
266                     | "ExitProcess"
267                     => {
268                         let exp_abi = if link_name.as_str() == "exit" {
269                             Abi::C { unwind: false }
270                         } else {
271                             Abi::System { unwind: false }
272                         };
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()));
277                     }
278                     "abort" => {
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()
282                         ))
283                     }
284                     _ => {
285                         if let Some(body) = this.lookup_exported_symbol(link_name)? {
286                             return Ok(Some(body));
287                         }
288                         this.handle_unsupported(format!(
289                             "can't call (diverging) foreign function: {}",
290                             link_name
291                         ))?;
292                         return Ok(None);
293                     }
294                 },
295             Some(p) => p,
296         };
297
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);
303             }
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));
309                 }
310
311                 this.handle_unsupported(format!("can't call foreign function: {}", link_name))?;
312                 return Ok(None);
313             }
314         }
315
316         Ok(None)
317     }
318
319     /// Emulates calling the internal __rust_* allocator functions
320     fn emulate_allocator(
321         &mut self,
322         symbol: Symbol,
323         default: impl FnOnce(&mut MiriEvalContext<'mir, 'tcx>) -> InterpResult<'tcx>,
324     ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
325         let this = self.eval_context_mut();
326
327         let allocator_kind = if let Some(allocator_kind) = this.tcx.allocator_kind(()) {
328             allocator_kind
329         } else {
330             // in real code, this symbol does not exist without an allocator
331             return Ok(EmulateByNameResult::NotSupported);
332         };
333
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");
339
340                 Ok(EmulateByNameResult::MirBody(body, instance))
341             }
342             AllocatorKind::Default => {
343                 default(this)?;
344                 Ok(EmulateByNameResult::NeedsJumping)
345             }
346         }
347     }
348
349     /// Emulates calling a foreign item using its name.
350     fn emulate_foreign_item_by_name(
351         &mut self,
352         link_name: Symbol,
353         abi: Abi,
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();
359
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");
370                 }
371                 this.machine.static_roots.push(alloc_id);
372             }
373
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)?;
377             }
378
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)?;
383             }
384
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)?;
389             }
390
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)?;
394             }
395
396             // Standard C allocation
397             "malloc" => {
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)?;
402             }
403             "calloc" => {
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)?;
407                 let size =
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)?;
411             }
412             "free" => {
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)?;
416             }
417             "realloc" => {
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)?;
423             }
424
425             // Rust allocation
426             "__rust_alloc" => {
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)?;
430
431                 return this.emulate_allocator(Symbol::intern("__rg_alloc"), |this| {
432                     Self::check_alloc_request(size, align)?;
433
434                     let ptr = this.allocate_ptr(
435                         Size::from_bytes(size),
436                         Align::from_bytes(align).unwrap(),
437                         MiriMemoryKind::Rust.into(),
438                     )?;
439
440                     this.write_pointer(ptr, dest)
441                 });
442             }
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)?;
447
448                 return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| {
449                     Self::check_alloc_request(size, align)?;
450
451                     let ptr = this.allocate_ptr(
452                         Size::from_bytes(size),
453                         Align::from_bytes(align).unwrap(),
454                         MiriMemoryKind::Rust.into(),
455                     )?;
456
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)
460                 });
461             }
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)?;
467
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.
470                     this.deallocate_ptr(
471                         ptr,
472                         Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
473                         MiriMemoryKind::Rust.into(),
474                     )
475                 });
476             }
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.
484
485                 return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| {
486                     Self::check_alloc_request(new_size, align)?;
487
488                     let align = Align::from_bytes(align).unwrap();
489                     let new_ptr = this.reallocate_ptr(
490                         ptr,
491                         Some((Size::from_bytes(old_size), align)),
492                         Size::from_bytes(new_size),
493                         align,
494                         MiriMemoryKind::Rust.into(),
495                     )?;
496                     this.write_pointer(new_ptr, dest)
497                 });
498             }
499
500             // C memory handling functions
501             "memcmp" => {
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)?);
506
507                 let result = {
508                     let left_bytes = this.read_bytes_ptr(left, n)?;
509                     let right_bytes = this.read_bytes_ptr(right, n)?;
510
511                     use std::cmp::Ordering::*;
512                     match left_bytes.cmp(right_bytes) {
513                         Less => -1i32,
514                         Equal => 0,
515                         Greater => 1,
516                     }
517                 };
518
519                 this.write_scalar(Scalar::from_i32(result), dest)?;
520             }
521             "memrchr" => {
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))?
528                     .iter()
529                     .rev()
530                     .position(|&c| c == val)
531                 {
532                     let new_ptr = ptr.offset(Size::from_bytes(num - idx as u64 - 1), this)?;
533                     this.write_pointer(new_ptr, dest)?;
534                 } else {
535                     this.write_null(dest)?;
536                 }
537             }
538             "memchr" => {
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)?;
543                 let idx = this
544                     .read_bytes_ptr(ptr, Size::from_bytes(num))?
545                     .iter()
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)?;
550                 } else {
551                     this.write_null(dest)?;
552                 }
553             }
554             "strlen" => {
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)?;
559             }
560
561             // math functions
562             #[rustfmt::skip]
563             | "cbrtf"
564             | "coshf"
565             | "sinhf"
566             | "tanf"
567             | "acosf"
568             | "asinf"
569             | "atanf"
570             => {
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() {
575                     "cbrtf" => f.cbrt(),
576                     "coshf" => f.cosh(),
577                     "sinhf" => f.sinh(),
578                     "tanf" => f.tan(),
579                     "acosf" => f.acos(),
580                     "asinf" => f.asin(),
581                     "atanf" => f.atan(),
582                     _ => bug!(),
583                 };
584                 this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
585             }
586             #[rustfmt::skip]
587             | "_hypotf"
588             | "hypotf"
589             | "atan2f"
590             => {
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),
600                     _ => bug!(),
601                 };
602                 this.write_scalar(Scalar::from_u32(n.to_bits()), dest)?;
603             }
604             #[rustfmt::skip]
605             | "cbrt"
606             | "cosh"
607             | "sinh"
608             | "tan"
609             | "acos"
610             | "asin"
611             | "atan"
612             => {
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() {
617                     "cbrt" => f.cbrt(),
618                     "cosh" => f.cosh(),
619                     "sinh" => f.sinh(),
620                     "tan" => f.tan(),
621                     "acos" => f.acos(),
622                     "asin" => f.asin(),
623                     "atan" => f.atan(),
624                     _ => bug!(),
625                 };
626                 this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
627             }
628             #[rustfmt::skip]
629             | "_hypot"
630             | "hypot"
631             | "atan2"
632             => {
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),
640                     _ => bug!(),
641                 };
642                 this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
643             }
644             #[rustfmt::skip]
645             | "_ldexp"
646             | "ldexp"
647             | "scalbn"
648             => {
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()?;
653
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) {
657                     i16::MAX
658                 } else if exp < i32::from(i16::MIN) {
659                     i16::MIN
660                 } else {
661                     exp.try_into().unwrap()
662                 };
663
664                 let res = x.scalbn(exp);
665                 this.write_scalar(Scalar::from_f64(res), dest)?;
666             }
667
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()?;
675
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>());
678
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)?;
683             }
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();
687             }
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()?;
691                 match arg {
692                     15 => { // SY ("full system scope")
693                         this.yield_active_thread();
694                     }
695                     _ => {
696                         throw_unsup_format!("unsupported llvm.aarch64.isb argument {}", arg);
697                     }
698                 }
699             }
700
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),
706             }
707         };
708
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)
712     }
713
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> {
717         if size == 0 {
718             throw_ub_format!("creating allocation with size 0");
719         }
720         if !align.is_power_of_two() {
721             throw_ub_format!("creating allocation with non-power-of-two alignment {}", align);
722         }
723         Ok(())
724     }
725 }