]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/interpret/intrinsics.rs
Auto merge of #79893 - RalfJung:forget-windows, r=oli-obk
[rust.git] / compiler / rustc_mir / src / interpret / intrinsics.rs
1 //! Intrinsics and other functions that the miri engine executes without
2 //! looking at their MIR. Intrinsics/functions supported here are shared by CTFE
3 //! and miri.
4
5 use std::convert::TryFrom;
6
7 use rustc_hir::def_id::DefId;
8 use rustc_middle::mir::{
9     self,
10     interpret::{uabs, ConstValue, GlobalId, InterpResult, Scalar},
11     BinOp,
12 };
13 use rustc_middle::ty;
14 use rustc_middle::ty::subst::SubstsRef;
15 use rustc_middle::ty::{Ty, TyCtxt};
16 use rustc_span::symbol::{sym, Symbol};
17 use rustc_target::abi::{Abi, LayoutOf as _, Primitive, Size};
18
19 use super::{
20     util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, OpTy, PlaceTy,
21 };
22
23 mod caller_location;
24 mod type_name;
25
26 fn numeric_intrinsic<'tcx, Tag>(
27     name: Symbol,
28     bits: u128,
29     kind: Primitive,
30 ) -> InterpResult<'tcx, Scalar<Tag>> {
31     let size = match kind {
32         Primitive::Int(integer, _) => integer.size(),
33         _ => bug!("invalid `{}` argument: {:?}", name, bits),
34     };
35     let extra = 128 - u128::from(size.bits());
36     let bits_out = match name {
37         sym::ctpop => u128::from(bits.count_ones()),
38         sym::ctlz => u128::from(bits.leading_zeros()) - extra,
39         sym::cttz => u128::from((bits << extra).trailing_zeros()) - extra,
40         sym::bswap => (bits << extra).swap_bytes(),
41         sym::bitreverse => (bits << extra).reverse_bits(),
42         _ => bug!("not a numeric intrinsic: {}", name),
43     };
44     Ok(Scalar::from_uint(bits_out, size))
45 }
46
47 /// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated
48 /// inside an `InterpCx` and instead have their value computed directly from rustc internal info.
49 crate fn eval_nullary_intrinsic<'tcx>(
50     tcx: TyCtxt<'tcx>,
51     param_env: ty::ParamEnv<'tcx>,
52     def_id: DefId,
53     substs: SubstsRef<'tcx>,
54 ) -> InterpResult<'tcx, ConstValue<'tcx>> {
55     let tp_ty = substs.type_at(0);
56     let name = tcx.item_name(def_id);
57     Ok(match name {
58         sym::type_name => {
59             ensure_monomorphic_enough(tcx, tp_ty)?;
60             let alloc = type_name::alloc_type_name(tcx, tp_ty);
61             ConstValue::Slice { data: alloc, start: 0, end: alloc.len() }
62         }
63         sym::needs_drop => ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env)),
64         sym::size_of | sym::min_align_of | sym::pref_align_of => {
65             let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
66             let n = match name {
67                 sym::pref_align_of => layout.align.pref.bytes(),
68                 sym::min_align_of => layout.align.abi.bytes(),
69                 sym::size_of => layout.size.bytes(),
70                 _ => bug!(),
71             };
72             ConstValue::from_machine_usize(n, &tcx)
73         }
74         sym::type_id => {
75             ensure_monomorphic_enough(tcx, tp_ty)?;
76             ConstValue::from_u64(tcx.type_id_hash(tp_ty))
77         }
78         sym::variant_count => match tp_ty.kind() {
79             ty::Adt(ref adt, _) => ConstValue::from_machine_usize(adt.variants.len() as u64, &tcx),
80             ty::Projection(_)
81             | ty::Opaque(_, _)
82             | ty::Param(_)
83             | ty::Bound(_, _)
84             | ty::Placeholder(_)
85             | ty::Infer(_) => throw_inval!(TooGeneric),
86             ty::Bool
87             | ty::Char
88             | ty::Int(_)
89             | ty::Uint(_)
90             | ty::Float(_)
91             | ty::Foreign(_)
92             | ty::Str
93             | ty::Array(_, _)
94             | ty::Slice(_)
95             | ty::RawPtr(_)
96             | ty::Ref(_, _, _)
97             | ty::FnDef(_, _)
98             | ty::FnPtr(_)
99             | ty::Dynamic(_, _)
100             | ty::Closure(_, _)
101             | ty::Generator(_, _, _)
102             | ty::GeneratorWitness(_)
103             | ty::Never
104             | ty::Tuple(_)
105             | ty::Error(_) => ConstValue::from_machine_usize(0u64, &tcx),
106         },
107         other => bug!("`{}` is not a zero arg intrinsic", other),
108     })
109 }
110
111 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
112     /// Returns `true` if emulation happened.
113     /// Here we implement the intrinsics that are common to all Miri instances; individual machines can add their own
114     /// intrinsic handling.
115     pub fn emulate_intrinsic(
116         &mut self,
117         instance: ty::Instance<'tcx>,
118         args: &[OpTy<'tcx, M::PointerTag>],
119         ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
120     ) -> InterpResult<'tcx, bool> {
121         let substs = instance.substs;
122         let intrinsic_name = self.tcx.item_name(instance.def_id());
123
124         // First handle intrinsics without return place.
125         let (dest, ret) = match ret {
126             None => match intrinsic_name {
127                 sym::transmute => throw_ub_format!("transmuting to uninhabited type"),
128                 sym::unreachable => throw_ub!(Unreachable),
129                 sym::abort => M::abort(self, "the program aborted execution".to_owned())?,
130                 // Unsupported diverging intrinsic.
131                 _ => return Ok(false),
132             },
133             Some(p) => p,
134         };
135
136         // Keep the patterns in this match ordered the same as the list in
137         // `src/librustc_middle/ty/constness.rs`
138         match intrinsic_name {
139             sym::caller_location => {
140                 let span = self.find_closest_untracked_caller_location();
141                 let location = self.alloc_caller_location_for_span(span);
142                 self.write_scalar(location.ptr, dest)?;
143             }
144
145             sym::min_align_of_val | sym::size_of_val => {
146                 let place = self.deref_operand(args[0])?;
147                 let (size, align) = self
148                     .size_and_align_of(place.meta, place.layout)?
149                     .ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;
150
151                 let result = match intrinsic_name {
152                     sym::min_align_of_val => align.bytes(),
153                     sym::size_of_val => size.bytes(),
154                     _ => bug!(),
155                 };
156
157                 self.write_scalar(Scalar::from_machine_usize(result, self), dest)?;
158             }
159
160             sym::min_align_of
161             | sym::pref_align_of
162             | sym::needs_drop
163             | sym::size_of
164             | sym::type_id
165             | sym::type_name
166             | sym::variant_count => {
167                 let gid = GlobalId { instance, promoted: None };
168                 let ty = match intrinsic_name {
169                     sym::min_align_of | sym::pref_align_of | sym::size_of | sym::variant_count => {
170                         self.tcx.types.usize
171                     }
172                     sym::needs_drop => self.tcx.types.bool,
173                     sym::type_id => self.tcx.types.u64,
174                     sym::type_name => self.tcx.mk_static_str(),
175                     _ => bug!("already checked for nullary intrinsics"),
176                 };
177                 let val =
178                     self.tcx.const_eval_global_id(self.param_env, gid, Some(self.tcx.span))?;
179                 let const_ = ty::Const { val: ty::ConstKind::Value(val), ty };
180                 let val = self.const_to_op(&const_, None)?;
181                 self.copy_op(val, dest)?;
182             }
183
184             sym::ctpop
185             | sym::cttz
186             | sym::cttz_nonzero
187             | sym::ctlz
188             | sym::ctlz_nonzero
189             | sym::bswap
190             | sym::bitreverse => {
191                 let ty = substs.type_at(0);
192                 let layout_of = self.layout_of(ty)?;
193                 let val = self.read_scalar(args[0])?.check_init()?;
194                 let bits = self.force_bits(val, layout_of.size)?;
195                 let kind = match layout_of.abi {
196                     Abi::Scalar(ref scalar) => scalar.value,
197                     _ => span_bug!(
198                         self.cur_span(),
199                         "{} called on invalid type {:?}",
200                         intrinsic_name,
201                         ty
202                     ),
203                 };
204                 let (nonzero, intrinsic_name) = match intrinsic_name {
205                     sym::cttz_nonzero => (true, sym::cttz),
206                     sym::ctlz_nonzero => (true, sym::ctlz),
207                     other => (false, other),
208                 };
209                 if nonzero && bits == 0 {
210                     throw_ub_format!("`{}_nonzero` called on 0", intrinsic_name);
211                 }
212                 let out_val = numeric_intrinsic(intrinsic_name, bits, kind)?;
213                 self.write_scalar(out_val, dest)?;
214             }
215             sym::wrapping_add
216             | sym::wrapping_sub
217             | sym::wrapping_mul
218             | sym::add_with_overflow
219             | sym::sub_with_overflow
220             | sym::mul_with_overflow => {
221                 let lhs = self.read_immediate(args[0])?;
222                 let rhs = self.read_immediate(args[1])?;
223                 let (bin_op, ignore_overflow) = match intrinsic_name {
224                     sym::wrapping_add => (BinOp::Add, true),
225                     sym::wrapping_sub => (BinOp::Sub, true),
226                     sym::wrapping_mul => (BinOp::Mul, true),
227                     sym::add_with_overflow => (BinOp::Add, false),
228                     sym::sub_with_overflow => (BinOp::Sub, false),
229                     sym::mul_with_overflow => (BinOp::Mul, false),
230                     _ => bug!("Already checked for int ops"),
231                 };
232                 if ignore_overflow {
233                     self.binop_ignore_overflow(bin_op, lhs, rhs, dest)?;
234                 } else {
235                     self.binop_with_overflow(bin_op, lhs, rhs, dest)?;
236                 }
237             }
238             sym::saturating_add | sym::saturating_sub => {
239                 let l = self.read_immediate(args[0])?;
240                 let r = self.read_immediate(args[1])?;
241                 let is_add = intrinsic_name == sym::saturating_add;
242                 let (val, overflowed, _ty) =
243                     self.overflowing_binary_op(if is_add { BinOp::Add } else { BinOp::Sub }, l, r)?;
244                 let val = if overflowed {
245                     let num_bits = l.layout.size.bits();
246                     if l.layout.abi.is_signed() {
247                         // For signed ints the saturated value depends on the sign of the first
248                         // term since the sign of the second term can be inferred from this and
249                         // the fact that the operation has overflowed (if either is 0 no
250                         // overflow can occur)
251                         let first_term: u128 = self.force_bits(l.to_scalar()?, l.layout.size)?;
252                         let first_term_positive = first_term & (1 << (num_bits - 1)) == 0;
253                         if first_term_positive {
254                             // Negative overflow not possible since the positive first term
255                             // can only increase an (in range) negative term for addition
256                             // or corresponding negated positive term for subtraction
257                             Scalar::from_uint(
258                                 (1u128 << (num_bits - 1)) - 1, // max positive
259                                 Size::from_bits(num_bits),
260                             )
261                         } else {
262                             // Positive overflow not possible for similar reason
263                             // max negative
264                             Scalar::from_uint(1u128 << (num_bits - 1), Size::from_bits(num_bits))
265                         }
266                     } else {
267                         // unsigned
268                         if is_add {
269                             // max unsigned
270                             Scalar::from_uint(
271                                 u128::MAX >> (128 - num_bits),
272                                 Size::from_bits(num_bits),
273                             )
274                         } else {
275                             // underflow to 0
276                             Scalar::from_uint(0u128, Size::from_bits(num_bits))
277                         }
278                     }
279                 } else {
280                     val
281                 };
282                 self.write_scalar(val, dest)?;
283             }
284             sym::discriminant_value => {
285                 let place = self.deref_operand(args[0])?;
286                 let discr_val = self.read_discriminant(place.into())?.0;
287                 self.write_scalar(discr_val, dest)?;
288             }
289             sym::unchecked_shl
290             | sym::unchecked_shr
291             | sym::unchecked_add
292             | sym::unchecked_sub
293             | sym::unchecked_mul
294             | sym::unchecked_div
295             | sym::unchecked_rem => {
296                 let l = self.read_immediate(args[0])?;
297                 let r = self.read_immediate(args[1])?;
298                 let bin_op = match intrinsic_name {
299                     sym::unchecked_shl => BinOp::Shl,
300                     sym::unchecked_shr => BinOp::Shr,
301                     sym::unchecked_add => BinOp::Add,
302                     sym::unchecked_sub => BinOp::Sub,
303                     sym::unchecked_mul => BinOp::Mul,
304                     sym::unchecked_div => BinOp::Div,
305                     sym::unchecked_rem => BinOp::Rem,
306                     _ => bug!("Already checked for int ops"),
307                 };
308                 let (val, overflowed, _ty) = self.overflowing_binary_op(bin_op, l, r)?;
309                 if overflowed {
310                     let layout = self.layout_of(substs.type_at(0))?;
311                     let r_val = self.force_bits(r.to_scalar()?, layout.size)?;
312                     if let sym::unchecked_shl | sym::unchecked_shr = intrinsic_name {
313                         throw_ub_format!("overflowing shift by {} in `{}`", r_val, intrinsic_name);
314                     } else {
315                         throw_ub_format!("overflow executing `{}`", intrinsic_name);
316                     }
317                 }
318                 self.write_scalar(val, dest)?;
319             }
320             sym::rotate_left | sym::rotate_right => {
321                 // rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
322                 // rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
323                 let layout = self.layout_of(substs.type_at(0))?;
324                 let val = self.read_scalar(args[0])?.check_init()?;
325                 let val_bits = self.force_bits(val, layout.size)?;
326                 let raw_shift = self.read_scalar(args[1])?.check_init()?;
327                 let raw_shift_bits = self.force_bits(raw_shift, layout.size)?;
328                 let width_bits = u128::from(layout.size.bits());
329                 let shift_bits = raw_shift_bits % width_bits;
330                 let inv_shift_bits = (width_bits - shift_bits) % width_bits;
331                 let result_bits = if intrinsic_name == sym::rotate_left {
332                     (val_bits << shift_bits) | (val_bits >> inv_shift_bits)
333                 } else {
334                     (val_bits >> shift_bits) | (val_bits << inv_shift_bits)
335                 };
336                 let truncated_bits = self.truncate(result_bits, layout);
337                 let result = Scalar::from_uint(truncated_bits, layout.size);
338                 self.write_scalar(result, dest)?;
339             }
340             sym::offset => {
341                 let ptr = self.read_scalar(args[0])?.check_init()?;
342                 let offset_count = self.read_scalar(args[1])?.to_machine_isize(self)?;
343                 let pointee_ty = substs.type_at(0);
344
345                 let offset_ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?;
346                 self.write_scalar(offset_ptr, dest)?;
347             }
348             sym::arith_offset => {
349                 let ptr = self.read_scalar(args[0])?.check_init()?;
350                 let offset_count = self.read_scalar(args[1])?.to_machine_isize(self)?;
351                 let pointee_ty = substs.type_at(0);
352
353                 let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
354                 let offset_bytes = offset_count.wrapping_mul(pointee_size);
355                 let offset_ptr = ptr.ptr_wrapping_signed_offset(offset_bytes, self);
356                 self.write_scalar(offset_ptr, dest)?;
357             }
358             sym::ptr_offset_from => {
359                 let a = self.read_immediate(args[0])?.to_scalar()?;
360                 let b = self.read_immediate(args[1])?.to_scalar()?;
361
362                 // Special case: if both scalars are *equal integers*
363                 // and not NULL, we pretend there is an allocation of size 0 right there,
364                 // and their offset is 0. (There's never a valid object at NULL, making it an
365                 // exception from the exception.)
366                 // This is the dual to the special exception for offset-by-0
367                 // in the inbounds pointer offset operation (see the Miri code, `src/operator.rs`).
368                 //
369                 // Control flow is weird because we cannot early-return (to reach the
370                 // `go_to_block` at the end).
371                 let done = if a.is_bits() && b.is_bits() {
372                     let a = a.to_machine_usize(self)?;
373                     let b = b.to_machine_usize(self)?;
374                     if a == b && a != 0 {
375                         self.write_scalar(Scalar::from_machine_isize(0, self), dest)?;
376                         true
377                     } else {
378                         false
379                     }
380                 } else {
381                     false
382                 };
383
384                 if !done {
385                     // General case: we need two pointers.
386                     let a = self.force_ptr(a)?;
387                     let b = self.force_ptr(b)?;
388                     if a.alloc_id != b.alloc_id {
389                         throw_ub_format!(
390                             "ptr_offset_from cannot compute offset of pointers into different \
391                             allocations.",
392                         );
393                     }
394                     let usize_layout = self.layout_of(self.tcx.types.usize)?;
395                     let isize_layout = self.layout_of(self.tcx.types.isize)?;
396                     let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout);
397                     let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout);
398                     let (val, _overflowed, _ty) =
399                         self.overflowing_binary_op(BinOp::Sub, a_offset, b_offset)?;
400                     let pointee_layout = self.layout_of(substs.type_at(0))?;
401                     let val = ImmTy::from_scalar(val, isize_layout);
402                     let size = ImmTy::from_int(pointee_layout.size.bytes(), isize_layout);
403                     self.exact_div(val, size, dest)?;
404                 }
405             }
406
407             sym::transmute => {
408                 self.copy_op_transmute(args[0], dest)?;
409             }
410             sym::assert_inhabited => {
411                 let ty = instance.substs.type_at(0);
412                 let layout = self.layout_of(ty)?;
413
414                 if layout.abi.is_uninhabited() {
415                     // The run-time intrinsic panics just to get a good backtrace; here we abort
416                     // since there is no problem showing a backtrace even for aborts.
417                     M::abort(self, format!("attempted to instantiate uninhabited type `{}`", ty))?;
418                 }
419             }
420             sym::simd_insert => {
421                 let index = u64::from(self.read_scalar(args[1])?.to_u32()?);
422                 let elem = args[2];
423                 let input = args[0];
424                 let (len, e_ty) = input.layout.ty.simd_size_and_type(*self.tcx);
425                 assert!(
426                     index < len,
427                     "Index `{}` must be in bounds of vector type `{}`: `[0, {})`",
428                     index,
429                     e_ty,
430                     len
431                 );
432                 assert_eq!(
433                     input.layout, dest.layout,
434                     "Return type `{}` must match vector type `{}`",
435                     dest.layout.ty, input.layout.ty
436                 );
437                 assert_eq!(
438                     elem.layout.ty, e_ty,
439                     "Scalar element type `{}` must match vector element type `{}`",
440                     elem.layout.ty, e_ty
441                 );
442
443                 for i in 0..len {
444                     let place = self.place_index(dest, i)?;
445                     let value = if i == index { elem } else { self.operand_index(input, i)? };
446                     self.copy_op(value, place)?;
447                 }
448             }
449             sym::simd_extract => {
450                 let index = u64::from(self.read_scalar(args[1])?.to_u32()?);
451                 let (len, e_ty) = args[0].layout.ty.simd_size_and_type(*self.tcx);
452                 assert!(
453                     index < len,
454                     "index `{}` is out-of-bounds of vector type `{}` with length `{}`",
455                     index,
456                     e_ty,
457                     len
458                 );
459                 assert_eq!(
460                     e_ty, dest.layout.ty,
461                     "Return type `{}` must match vector element type `{}`",
462                     dest.layout.ty, e_ty
463                 );
464                 self.copy_op(self.operand_index(args[0], index)?, dest)?;
465             }
466             sym::likely | sym::unlikely => {
467                 // These just return their argument
468                 self.copy_op(args[0], dest)?;
469             }
470             sym::assume => {
471                 let cond = self.read_scalar(args[0])?.check_init()?.to_bool()?;
472                 if !cond {
473                     throw_ub_format!("`assume` intrinsic called with `false`");
474                 }
475             }
476             _ => return Ok(false),
477         }
478
479         trace!("{:?}", self.dump_place(*dest));
480         self.go_to_block(ret);
481         Ok(true)
482     }
483
484     pub fn exact_div(
485         &mut self,
486         a: ImmTy<'tcx, M::PointerTag>,
487         b: ImmTy<'tcx, M::PointerTag>,
488         dest: PlaceTy<'tcx, M::PointerTag>,
489     ) -> InterpResult<'tcx> {
490         // Performs an exact division, resulting in undefined behavior where
491         // `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`.
492         // First, check x % y != 0 (or if that computation overflows).
493         let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, a, b)?;
494         if overflow || res.assert_bits(a.layout.size) != 0 {
495             // Then, check if `b` is -1, which is the "MIN / -1" case.
496             let minus1 = Scalar::from_int(-1, dest.layout.size);
497             let b_scalar = b.to_scalar().unwrap();
498             if b_scalar == minus1 {
499                 throw_ub_format!("exact_div: result of dividing MIN by -1 cannot be represented")
500             } else {
501                 throw_ub_format!("exact_div: {} cannot be divided by {} without remainder", a, b,)
502             }
503         }
504         // `Rem` says this is all right, so we can let `Div` do its job.
505         self.binop_ignore_overflow(BinOp::Div, a, b, dest)
506     }
507
508     /// Offsets a pointer by some multiple of its type, returning an error if the pointer leaves its
509     /// allocation. For integer pointers, we consider each of them their own tiny allocation of size
510     /// 0, so offset-by-0 (and only 0) is okay -- except that NULL cannot be offset by _any_ value.
511     pub fn ptr_offset_inbounds(
512         &self,
513         ptr: Scalar<M::PointerTag>,
514         pointee_ty: Ty<'tcx>,
515         offset_count: i64,
516     ) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
517         // We cannot overflow i64 as a type's size must be <= isize::MAX.
518         let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
519         // The computed offset, in bytes, cannot overflow an isize.
520         let offset_bytes =
521             offset_count.checked_mul(pointee_size).ok_or(err_ub!(PointerArithOverflow))?;
522         // The offset being in bounds cannot rely on "wrapping around" the address space.
523         // So, first rule out overflows in the pointer arithmetic.
524         let offset_ptr = ptr.ptr_signed_offset(offset_bytes, self)?;
525         // ptr and offset_ptr must be in bounds of the same allocated object. This means all of the
526         // memory between these pointers must be accessible. Note that we do not require the
527         // pointers to be properly aligned (unlike a read/write operation).
528         let min_ptr = if offset_bytes >= 0 { ptr } else { offset_ptr };
529         let size: u64 = uabs(offset_bytes);
530         // This call handles checking for integer/NULL pointers.
531         self.memory.check_ptr_access_align(
532             min_ptr,
533             Size::from_bytes(size),
534             None,
535             CheckInAllocMsg::InboundsTest,
536         )?;
537         Ok(offset_ptr)
538     }
539 }