]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items.rs
Auto merge of #2047 - RalfJung:no-extras, r=RalfJung
[rust.git] / src / shims / foreign_items.rs
1 use std::{
2     collections::hash_map::Entry,
3     convert::{TryFrom, TryInto},
4     iter,
5 };
6
7 use log::trace;
8
9 use rustc_apfloat::Float;
10 use rustc_ast::expand::allocator::AllocatorKind;
11 use rustc_hir::{
12     def::DefKind,
13     def_id::{CrateNum, DefId, LOCAL_CRATE},
14 };
15 use rustc_middle::middle::{
16     codegen_fn_attrs::CodegenFnAttrFlags, dependency_format::Linkage,
17     exported_symbols::ExportedSymbol,
18 };
19 use rustc_middle::mir;
20 use rustc_middle::ty;
21 use rustc_session::config::CrateType;
22 use rustc_span::{symbol::sym, Symbol};
23 use rustc_target::{
24     abi::{Align, Size},
25     spec::abi::Abi,
26 };
27
28 use super::backtrace::EvalContextExt as _;
29 use crate::*;
30
31 /// Returned by `emulate_foreign_item_by_name`.
32 pub enum EmulateByNameResult<'mir, 'tcx> {
33     /// The caller is expected to jump to the return block.
34     NeedsJumping,
35     /// Jumping has already been taken care of.
36     AlreadyJumped,
37     /// A MIR body has been found for the function
38     MirBody(&'mir mir::Body<'tcx>, ty::Instance<'tcx>),
39     /// The item is not supported.
40     NotSupported,
41 }
42
43 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
44 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
45     /// Returns the minimum alignment for the target architecture for allocations of the given size.
46     fn min_align(&self, size: u64, kind: MiriMemoryKind) -> Align {
47         let this = self.eval_context_ref();
48         // List taken from `libstd/sys_common/alloc.rs`.
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),
53         };
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();
59         }
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();
63             if next_pow2 == x {
64                 // x *is* a power of two, just use that.
65                 x
66             } else {
67                 // x is between two powers, so next = 2*prev.
68                 next_pow2 / 2
69             }
70         }
71         Align::from_bytes(prev_power_of_two(size)).unwrap()
72     }
73
74     fn malloc(
75         &mut self,
76         size: u64,
77         zero_init: bool,
78         kind: MiriMemoryKind,
79     ) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
80         let this = self.eval_context_mut();
81         if size == 0 {
82             Ok(Pointer::null())
83         } else {
84             let align = this.min_align(size, kind);
85             let ptr = this.allocate_ptr(Size::from_bytes(size), align, kind.into())?;
86             if zero_init {
87                 // We just allocated this, the access is definitely in-bounds.
88                 this.write_bytes_ptr(ptr.into(), iter::repeat(0u8).take(size as usize)).unwrap();
89             }
90             Ok(ptr.into())
91         }
92     }
93
94     fn free(&mut self, ptr: Pointer<Option<Tag>>, kind: MiriMemoryKind) -> InterpResult<'tcx> {
95         let this = self.eval_context_mut();
96         if !this.ptr_is_null(ptr)? {
97             this.deallocate_ptr(ptr, None, kind.into())?;
98         }
99         Ok(())
100     }
101
102     fn realloc(
103         &mut self,
104         old_ptr: Pointer<Option<Tag>>,
105         new_size: u64,
106         kind: MiriMemoryKind,
107     ) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
108         let this = self.eval_context_mut();
109         let new_align = this.min_align(new_size, kind);
110         if this.ptr_is_null(old_ptr)? {
111             if new_size == 0 {
112                 Ok(Pointer::null())
113             } else {
114                 let new_ptr =
115                     this.allocate_ptr(Size::from_bytes(new_size), new_align, kind.into())?;
116                 Ok(new_ptr.into())
117             }
118         } else {
119             if new_size == 0 {
120                 this.deallocate_ptr(old_ptr, None, kind.into())?;
121                 Ok(Pointer::null())
122             } else {
123                 let new_ptr = this.reallocate_ptr(
124                     old_ptr,
125                     None,
126                     Size::from_bytes(new_size),
127                     new_align,
128                     kind.into(),
129                 )?;
130                 Ok(new_ptr.into())
131             }
132         }
133     }
134
135     /// Lookup the body of a function that has `link_name` as the symbol name.
136     fn lookup_exported_symbol(
137         &mut self,
138         link_name: Symbol,
139     ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
140         let this = self.eval_context_mut();
141         let tcx = this.tcx.tcx;
142
143         // If the result was cached, just return it.
144         // (Cannot use `or_insert` since the code below might have to throw an error.)
145         let entry = this.machine.exported_symbols_cache.entry(link_name);
146         let instance = *match entry {
147             Entry::Occupied(e) => e.into_mut(),
148             Entry::Vacant(e) => {
149                 // Find it if it was not cached.
150                 let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
151                 // `dependency_formats` includes all the transitive informations needed to link a crate,
152                 // which is what we need here since we need to dig out `exported_symbols` from all transitive
153                 // dependencies.
154                 let dependency_formats = tcx.dependency_formats(());
155                 let dependency_format = dependency_formats
156                     .iter()
157                     .find(|(crate_type, _)| *crate_type == CrateType::Executable)
158                     .expect("interpreting a non-executable crate");
159                 for cnum in iter::once(LOCAL_CRATE).chain(
160                     dependency_format.1.iter().enumerate().filter_map(|(num, &linkage)| {
161                         (linkage != Linkage::NotLinked).then_some(CrateNum::new(num + 1))
162                     }),
163                 ) {
164                     // We can ignore `_export_level` here: we are a Rust crate, and everything is exported
165                     // from a Rust crate.
166                     for &(symbol, _export_level) in tcx.exported_symbols(cnum) {
167                         if let ExportedSymbol::NonGeneric(def_id) = symbol {
168                             let attrs = tcx.codegen_fn_attrs(def_id);
169                             let symbol_name = if let Some(export_name) = attrs.export_name {
170                                 export_name
171                             } else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
172                                 tcx.item_name(def_id)
173                             } else {
174                                 // Skip over items without an explicitly defined symbol name.
175                                 continue;
176                             };
177                             if symbol_name == link_name {
178                                 if let Some((original_instance, original_cnum)) = instance_and_crate
179                                 {
180                                     // Make sure we are consistent wrt what is 'first' and 'second'.
181                                     let original_span =
182                                         tcx.def_span(original_instance.def_id()).data();
183                                     let span = tcx.def_span(def_id).data();
184                                     if original_span < span {
185                                         throw_machine_stop!(
186                                             TerminationInfo::MultipleSymbolDefinitions {
187                                                 link_name,
188                                                 first: original_span,
189                                                 first_crate: tcx.crate_name(original_cnum),
190                                                 second: span,
191                                                 second_crate: tcx.crate_name(cnum),
192                                             }
193                                         );
194                                     } else {
195                                         throw_machine_stop!(
196                                             TerminationInfo::MultipleSymbolDefinitions {
197                                                 link_name,
198                                                 first: span,
199                                                 first_crate: tcx.crate_name(cnum),
200                                                 second: original_span,
201                                                 second_crate: tcx.crate_name(original_cnum),
202                                             }
203                                         );
204                                     }
205                                 }
206                                 if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
207                                     throw_ub_format!(
208                                         "attempt to call an exported symbol that is not defined as a function"
209                                     );
210                                 }
211                                 instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
212                             }
213                         }
214                     }
215                 }
216
217                 e.insert(instance_and_crate.map(|ic| ic.0))
218             }
219         };
220         match instance {
221             None => Ok(None), // no symbol with this name
222             Some(instance) => Ok(Some((this.load_mir(instance.def, None)?, instance))),
223         }
224     }
225
226     /// Emulates calling a foreign item, failing if the item is not supported.
227     /// This function will handle `goto_block` if needed.
228     /// Returns Ok(None) if the foreign item was completely handled
229     /// by this function.
230     /// Returns Ok(Some(body)) if processing the foreign item
231     /// is delegated to another function.
232     fn emulate_foreign_item(
233         &mut self,
234         def_id: DefId,
235         abi: Abi,
236         args: &[OpTy<'tcx, Tag>],
237         ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
238         unwind: StackPopUnwind,
239     ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
240         let this = self.eval_context_mut();
241         let attrs = this.tcx.get_attrs(def_id);
242         let link_name = this
243             .tcx
244             .sess
245             .first_attr_value_str_by_name(&attrs, sym::link_name)
246             .unwrap_or_else(|| this.tcx.item_name(def_id));
247         let tcx = this.tcx.tcx;
248
249         // First: functions that diverge.
250         let (dest, ret) = match ret {
251             None =>
252                 match &*link_name.as_str() {
253                     "miri_start_panic" => {
254                         // `check_shim` happens inside `handle_miri_start_panic`.
255                         this.handle_miri_start_panic(abi, link_name, args, unwind)?;
256                         return Ok(None);
257                     }
258                     // This matches calls to the foreign item `panic_impl`.
259                     // The implementation is provided by the function with the `#[panic_handler]` attribute.
260                     "panic_impl" => {
261                         // We don't use `check_shim` here because we are just forwarding to the lang
262                         // item. Argument count checking will be performed when the returned `Body` is
263                         // called.
264                         this.check_abi_and_shim_symbol_clash(abi, Abi::Rust, link_name)?;
265                         let panic_impl_id = tcx.lang_items().panic_impl().unwrap();
266                         let panic_impl_instance = ty::Instance::mono(tcx, panic_impl_id);
267                         return Ok(Some((
268                             &*this.load_mir(panic_impl_instance.def, None)?,
269                             panic_impl_instance,
270                         )));
271                     }
272                     #[rustfmt::skip]
273                     | "exit"
274                     | "ExitProcess"
275                     => {
276                         let exp_abi = if link_name.as_str() == "exit" {
277                             Abi::C { unwind: false }
278                         } else {
279                             Abi::System { unwind: false }
280                         };
281                         let &[ref code] = this.check_shim(abi, exp_abi, link_name, args)?;
282                         // it's really u32 for ExitProcess, but we have to put it into the `Exit` variant anyway
283                         let code = this.read_scalar(code)?.to_i32()?;
284                         throw_machine_stop!(TerminationInfo::Exit(code.into()));
285                     }
286                     "abort" => {
287                         let &[] =
288                             this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
289                         throw_machine_stop!(TerminationInfo::Abort(
290                             "the program aborted execution".to_owned()
291                         ))
292                     }
293                     _ => {
294                         if let Some(body) = this.lookup_exported_symbol(link_name)? {
295                             return Ok(Some(body));
296                         }
297                         this.handle_unsupported(format!(
298                             "can't call (diverging) foreign function: {}",
299                             link_name
300                         ))?;
301                         return Ok(None);
302                     }
303                 },
304             Some(p) => p,
305         };
306
307         // Second: functions that return.
308         match this.emulate_foreign_item_by_name(link_name, abi, args, dest, ret)? {
309             EmulateByNameResult::NeedsJumping => {
310                 trace!("{:?}", this.dump_place(**dest));
311                 this.go_to_block(ret);
312             }
313             EmulateByNameResult::AlreadyJumped => (),
314             EmulateByNameResult::MirBody(mir, instance) => return Ok(Some((mir, instance))),
315             EmulateByNameResult::NotSupported => {
316                 if let Some(body) = this.lookup_exported_symbol(link_name)? {
317                     return Ok(Some(body));
318                 }
319
320                 this.handle_unsupported(format!("can't call foreign function: {}", link_name))?;
321                 return Ok(None);
322             }
323         }
324
325         Ok(None)
326     }
327
328     /// Emulates calling the internal __rust_* allocator functions
329     fn emulate_allocator(
330         &mut self,
331         symbol: Symbol,
332         default: impl FnOnce(&mut MiriEvalContext<'mir, 'tcx>) -> InterpResult<'tcx>,
333     ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
334         let this = self.eval_context_mut();
335
336         let allocator_kind = if let Some(allocator_kind) = this.tcx.allocator_kind(()) {
337             allocator_kind
338         } else {
339             // in real code, this symbol does not exist without an allocator
340             return Ok(EmulateByNameResult::NotSupported);
341         };
342
343         match allocator_kind {
344             AllocatorKind::Global => {
345                 let (body, instance) = this
346                     .lookup_exported_symbol(symbol)?
347                     .expect("symbol should be present if there is a global allocator");
348
349                 Ok(EmulateByNameResult::MirBody(body, instance))
350             }
351             AllocatorKind::Default => {
352                 default(this)?;
353                 Ok(EmulateByNameResult::NeedsJumping)
354             }
355         }
356     }
357
358     /// Emulates calling a foreign item using its name.
359     fn emulate_foreign_item_by_name(
360         &mut self,
361         link_name: Symbol,
362         abi: Abi,
363         args: &[OpTy<'tcx, Tag>],
364         dest: &PlaceTy<'tcx, Tag>,
365         ret: mir::BasicBlock,
366     ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
367         let this = self.eval_context_mut();
368
369         // Here we dispatch all the shims for foreign functions. If you have a platform specific
370         // shim, add it to the corresponding submodule.
371         match &*link_name.as_str() {
372             // Miri-specific extern functions
373             "miri_static_root" => {
374                 let &[ref ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
375                 let ptr = this.read_pointer(ptr)?;
376                 let (alloc_id, offset, _) = this.ptr_get_alloc_id(ptr)?;
377                 if offset != Size::ZERO {
378                     throw_unsup_format!("pointer passed to miri_static_root must point to beginning of an allocated block");
379                 }
380                 this.machine.static_roots.push(alloc_id);
381             }
382
383             // Obtains the size of a Miri backtrace. See the README for details.
384             "miri_backtrace_size" => {
385                 this.handle_miri_backtrace_size(abi, link_name, args, dest)?;
386             }
387
388             // Obtains a Miri backtrace. See the README for details.
389             "miri_get_backtrace" => {
390                 // `check_shim` happens inside `handle_miri_get_backtrace`.
391                 this.handle_miri_get_backtrace(abi, link_name, args, dest)?;
392             }
393
394             // Resolves a Miri backtrace frame. See the README for details.
395             "miri_resolve_frame" => {
396                 // `check_shim` happens inside `handle_miri_resolve_frame`.
397                 this.handle_miri_resolve_frame(abi, link_name, args, dest)?;
398             }
399
400             // Writes the function and file names of a Miri backtrace frame into a user provided buffer. See the README for details.
401             "miri_resolve_frame_names" => {
402                 this.handle_miri_resolve_frame_names(abi, link_name, args)?;
403             }
404
405             // Standard C allocation
406             "malloc" => {
407                 let &[ref size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
408                 let size = this.read_scalar(size)?.to_machine_usize(this)?;
409                 let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C)?;
410                 this.write_pointer(res, dest)?;
411             }
412             "calloc" => {
413                 let &[ref items, ref len] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
414                 let items = this.read_scalar(items)?.to_machine_usize(this)?;
415                 let len = this.read_scalar(len)?.to_machine_usize(this)?;
416                 let size =
417                     items.checked_mul(len).ok_or_else(|| err_ub_format!("overflow during calloc size computation"))?;
418                 let res = this.malloc(size, /*zero_init:*/ true, MiriMemoryKind::C)?;
419                 this.write_pointer(res, dest)?;
420             }
421             "free" => {
422                 let &[ref ptr] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
423                 let ptr = this.read_pointer(ptr)?;
424                 this.free(ptr, MiriMemoryKind::C)?;
425             }
426             "realloc" => {
427                 let &[ref old_ptr, ref new_size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
428                 let old_ptr = this.read_pointer(old_ptr)?;
429                 let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
430                 let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?;
431                 this.write_pointer(res, dest)?;
432             }
433
434             // Rust allocation
435             "__rust_alloc" => {
436                 let &[ref size, ref align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
437                 let size = this.read_scalar(size)?.to_machine_usize(this)?;
438                 let align = this.read_scalar(align)?.to_machine_usize(this)?;
439
440                 return this.emulate_allocator(Symbol::intern("__rg_alloc"), |this| {
441                     Self::check_alloc_request(size, align)?;
442
443                     let ptr = this.allocate_ptr(
444                         Size::from_bytes(size),
445                         Align::from_bytes(align).unwrap(),
446                         MiriMemoryKind::Rust.into(),
447                     )?;
448
449                     this.write_pointer(ptr, dest)
450                 });
451             }
452             "__rust_alloc_zeroed" => {
453                 let &[ref size, ref align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
454                 let size = this.read_scalar(size)?.to_machine_usize(this)?;
455                 let align = this.read_scalar(align)?.to_machine_usize(this)?;
456
457                 return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| {
458                     Self::check_alloc_request(size, align)?;
459
460                     let ptr = this.allocate_ptr(
461                         Size::from_bytes(size),
462                         Align::from_bytes(align).unwrap(),
463                         MiriMemoryKind::Rust.into(),
464                     )?;
465
466                     // We just allocated this, the access is definitely in-bounds.
467                     this.write_bytes_ptr(ptr.into(), iter::repeat(0u8).take(usize::try_from(size).unwrap())).unwrap();
468                     this.write_pointer(ptr, dest)
469                 });
470             }
471             "__rust_dealloc" => {
472                 let &[ref ptr, ref old_size, ref align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
473                 let ptr = this.read_pointer(ptr)?;
474                 let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
475                 let align = this.read_scalar(align)?.to_machine_usize(this)?;
476
477                 return this.emulate_allocator(Symbol::intern("__rg_dealloc"), |this| {
478                     // No need to check old_size/align; we anyway check that they match the allocation.
479                     this.deallocate_ptr(
480                         ptr,
481                         Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
482                         MiriMemoryKind::Rust.into(),
483                     )
484                 });
485             }
486             "__rust_realloc" => {
487                 let &[ref ptr, ref old_size, ref align, ref new_size] = this.check_shim(abi, Abi::Rust, link_name, args)?;
488                 let ptr = this.read_pointer(ptr)?;
489                 let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
490                 let align = this.read_scalar(align)?.to_machine_usize(this)?;
491                 let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
492                 // No need to check old_size; we anyway check that they match the allocation.
493
494                 return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| {
495                     Self::check_alloc_request(new_size, align)?;
496
497                     let align = Align::from_bytes(align).unwrap();
498                     let new_ptr = this.reallocate_ptr(
499                         ptr,
500                         Some((Size::from_bytes(old_size), align)),
501                         Size::from_bytes(new_size),
502                         align,
503                         MiriMemoryKind::Rust.into(),
504                     )?;
505                     this.write_pointer(new_ptr, dest)
506                 });
507             }
508
509             // C memory handling functions
510             "memcmp" => {
511                 let &[ref left, ref right, ref n] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
512                 let left = this.read_pointer(left)?;
513                 let right = this.read_pointer(right)?;
514                 let n = Size::from_bytes(this.read_scalar(n)?.to_machine_usize(this)?);
515
516                 let result = {
517                     let left_bytes = this.read_bytes_ptr(left, n)?;
518                     let right_bytes = this.read_bytes_ptr(right, n)?;
519
520                     use std::cmp::Ordering::*;
521                     match left_bytes.cmp(right_bytes) {
522                         Less => -1i32,
523                         Equal => 0,
524                         Greater => 1,
525                     }
526                 };
527
528                 this.write_scalar(Scalar::from_i32(result), dest)?;
529             }
530             "memrchr" => {
531                 let &[ref ptr, ref val, ref num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
532                 let ptr = this.read_pointer(ptr)?;
533                 let val = this.read_scalar(val)?.to_i32()? as u8;
534                 let num = this.read_scalar(num)?.to_machine_usize(this)?;
535                 if let Some(idx) = this
536                     .read_bytes_ptr(ptr, Size::from_bytes(num))?
537                     .iter()
538                     .rev()
539                     .position(|&c| c == val)
540                 {
541                     let new_ptr = ptr.offset(Size::from_bytes(num - idx as u64 - 1), this)?;
542                     this.write_pointer(new_ptr, dest)?;
543                 } else {
544                     this.write_null(dest)?;
545                 }
546             }
547             "memchr" => {
548                 let &[ref ptr, ref val, ref num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
549                 let ptr = this.read_pointer(ptr)?;
550                 let val = this.read_scalar(val)?.to_i32()? as u8;
551                 let num = this.read_scalar(num)?.to_machine_usize(this)?;
552                 let idx = this
553                     .read_bytes_ptr(ptr, Size::from_bytes(num))?
554                     .iter()
555                     .position(|&c| c == val);
556                 if let Some(idx) = idx {
557                     let new_ptr = ptr.offset(Size::from_bytes(idx as u64), this)?;
558                     this.write_pointer(new_ptr, dest)?;
559                 } else {
560                     this.write_null(dest)?;
561                 }
562             }
563             "strlen" => {
564                 let &[ref ptr] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
565                 let ptr = this.read_pointer(ptr)?;
566                 let n = this.read_c_str(ptr)?.len();
567                 this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
568             }
569
570             // math functions
571             #[rustfmt::skip]
572             | "cbrtf"
573             | "coshf"
574             | "sinhf"
575             | "tanf"
576             | "acosf"
577             | "asinf"
578             | "atanf"
579             => {
580                 let &[ref f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
581                 // FIXME: Using host floats.
582                 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
583                 let f = match &*link_name.as_str() {
584                     "cbrtf" => f.cbrt(),
585                     "coshf" => f.cosh(),
586                     "sinhf" => f.sinh(),
587                     "tanf" => f.tan(),
588                     "acosf" => f.acos(),
589                     "asinf" => f.asin(),
590                     "atanf" => f.atan(),
591                     _ => bug!(),
592                 };
593                 this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
594             }
595             #[rustfmt::skip]
596             | "_hypotf"
597             | "hypotf"
598             | "atan2f"
599             => {
600                 let &[ref f1, ref f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
601                 // underscore case for windows, here and below
602                 // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
603                 // FIXME: Using host floats.
604                 let f1 = f32::from_bits(this.read_scalar(f1)?.to_u32()?);
605                 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
606                 let n = match &*link_name.as_str() {
607                     "_hypotf" | "hypotf" => f1.hypot(f2),
608                     "atan2f" => f1.atan2(f2),
609                     _ => bug!(),
610                 };
611                 this.write_scalar(Scalar::from_u32(n.to_bits()), dest)?;
612             }
613             #[rustfmt::skip]
614             | "cbrt"
615             | "cosh"
616             | "sinh"
617             | "tan"
618             | "acos"
619             | "asin"
620             | "atan"
621             => {
622                 let &[ref f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
623                 // FIXME: Using host floats.
624                 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
625                 let f = match &*link_name.as_str() {
626                     "cbrt" => f.cbrt(),
627                     "cosh" => f.cosh(),
628                     "sinh" => f.sinh(),
629                     "tan" => f.tan(),
630                     "acos" => f.acos(),
631                     "asin" => f.asin(),
632                     "atan" => f.atan(),
633                     _ => bug!(),
634                 };
635                 this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
636             }
637             #[rustfmt::skip]
638             | "_hypot"
639             | "hypot"
640             | "atan2"
641             => {
642                 let &[ref f1, ref f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
643                 // FIXME: Using host floats.
644                 let f1 = f64::from_bits(this.read_scalar(f1)?.to_u64()?);
645                 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
646                 let n = match &*link_name.as_str() {
647                     "_hypot" | "hypot" => f1.hypot(f2),
648                     "atan2" => f1.atan2(f2),
649                     _ => bug!(),
650                 };
651                 this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
652             }
653             #[rustfmt::skip]
654             | "_ldexp"
655             | "ldexp"
656             | "scalbn"
657             => {
658                 let &[ref x, ref exp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
659                 // For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
660                 let x = this.read_scalar(x)?.to_f64()?;
661                 let exp = this.read_scalar(exp)?.to_i32()?;
662
663                 // Saturating cast to i16. Even those are outside the valid exponent range to
664                 // `scalbn` below will do its over/underflow handling.
665                 let exp = if exp > i32::from(i16::MAX) {
666                     i16::MAX
667                 } else if exp < i32::from(i16::MIN) {
668                     i16::MIN
669                 } else {
670                     exp.try_into().unwrap()
671                 };
672
673                 let res = x.scalbn(exp);
674                 this.write_scalar(Scalar::from_f64(res), dest)?;
675             }
676
677             // Architecture-specific shims
678             "llvm.x86.sse2.pause" if this.tcx.sess.target.arch == "x86" || this.tcx.sess.target.arch == "x86_64" => {
679                 let &[] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
680                 this.yield_active_thread();
681             }
682             "llvm.aarch64.isb" if this.tcx.sess.target.arch == "aarch64" => {
683                 let &[ref arg] = this.check_shim(abi, Abi::Unadjusted, link_name, args)?;
684                 let arg = this.read_scalar(arg)?.to_i32()?;
685                 match arg {
686                     15 => { // SY ("full system scope")
687                         this.yield_active_thread();
688                     }
689                     _ => {
690                         throw_unsup_format!("unsupported llvm.aarch64.isb argument {}", arg);
691                     }
692                 }
693             }
694
695             // Platform-specific shims
696             _ => match this.tcx.sess.target.os.as_ref() {
697                 "linux" | "macos" => return shims::posix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
698                 "windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
699                 target => throw_unsup_format!("the target `{}` is not supported", target),
700             }
701         };
702
703         // We only fall through to here if we did *not* hit the `_` arm above,
704         // i.e., if we actually emulated the function.
705         Ok(EmulateByNameResult::NeedsJumping)
706     }
707
708     /// Check some basic requirements for this allocation request:
709     /// non-zero size, power-of-two alignment.
710     fn check_alloc_request(size: u64, align: u64) -> InterpResult<'tcx> {
711         if size == 0 {
712             throw_ub_format!("creating allocation with size 0");
713         }
714         if !align.is_power_of_two() {
715             throw_ub_format!("creating allocation with non-power-of-two alignment {}", align);
716         }
717         Ok(())
718     }
719 }