]> git.lizzy.rs Git - rust.git/blob - src/lib.rs
77acd3045da573c147d5601ee3c1f18932e6441d
[rust.git] / src / lib.rs
1 #![feature(rustc_private)]
2
3 #![allow(clippy::cast_lossless)]
4
5 #[macro_use]
6 extern crate log;
7 // From rustc.
8 extern crate syntax;
9 extern crate rustc_apfloat;
10 #[macro_use] extern crate rustc;
11 extern crate rustc_data_structures;
12 extern crate rustc_mir;
13 extern crate rustc_target;
14
15 mod fn_call;
16 mod operator;
17 mod intrinsic;
18 mod helpers;
19 mod tls;
20 mod range_map;
21 mod mono_hash_map;
22 mod stacked_borrows;
23 mod intptrcast;
24 mod memory;
25
26 use std::collections::HashMap;
27 use std::borrow::Cow;
28 use std::rc::Rc;
29
30 use rand::rngs::StdRng;
31 use rand::SeedableRng;
32
33 use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
34 use rustc::ty::layout::{LayoutOf, Size, Align};
35 use rustc::hir::def_id::DefId;
36 use rustc::mir;
37 pub use rustc_mir::interpret::*;
38 // Resolve ambiguity.
39 pub use rustc_mir::interpret::{self, AllocMap, PlaceTy};
40 use syntax::attr;
41 use syntax::source_map::DUMMY_SP;
42 use syntax::symbol::sym;
43
44 pub use crate::fn_call::EvalContextExt as MissingFnsEvalContextExt;
45 pub use crate::operator::EvalContextExt as OperatorEvalContextExt;
46 pub use crate::intrinsic::EvalContextExt as IntrinsicEvalContextExt;
47 pub use crate::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
48 use crate::range_map::RangeMap;
49 #[allow(unused_imports)] // FIXME: rustc bug, issue <https://github.com/rust-lang/rust/issues/53682>.
50 pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
51 use crate::mono_hash_map::MonoHashMap;
52 pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt};
53 use crate::memory::AllocExtra;
54
55 // Used by priroda.
56 pub use crate::stacked_borrows::{Tag, Permission, Stack, Stacks, Item};
57
58 /// Insert rustc arguments at the beginning of the argument list that Miri wants to be
59 /// set per default, for maximal validation power.
60 pub fn miri_default_args() -> &'static [&'static str] {
61     // The flags here should be kept in sync with what bootstrap adds when `test-miri` is
62     // set, which happens in `bootstrap/bin/rustc.rs` in the rustc sources.
63     &["-Zalways-encode-mir", "-Zmir-emit-retag", "-Zmir-opt-level=0", "--cfg=miri"]
64 }
65
66 /// Configuration needed to spawn a Miri instance.
67 #[derive(Clone)]
68 pub struct MiriConfig {
69     pub validate: bool,
70     pub args: Vec<String>,
71
72     // The seed to use when non-determinism is required (e.g. getrandom())
73     pub seed: Option<u64>
74 }
75
76 // Used by priroda.
77 pub fn create_ecx<'mir, 'tcx: 'mir>(
78     tcx: TyCtxt<'tcx>,
79     main_id: DefId,
80     config: MiriConfig,
81 ) -> InterpResult<'tcx, InterpretCx<'mir, 'tcx, Evaluator<'tcx>>> {
82     let mut ecx = InterpretCx::new(
83         tcx.at(syntax::source_map::DUMMY_SP),
84         ty::ParamEnv::reveal_all(),
85         Evaluator::new(config.validate),
86     );
87
88     ecx.memory_mut().extra.rng = config.seed.map(StdRng::seed_from_u64);
89     
90     let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
91     let main_mir = ecx.load_mir(main_instance.def)?;
92
93     if !main_mir.return_ty().is_unit() || main_mir.arg_count != 0 {
94         return err!(Unimplemented(
95             "miri does not support main functions without `fn()` type signatures"
96                 .to_owned(),
97         ));
98     }
99
100     let start_id = tcx.lang_items().start_fn().unwrap();
101     let main_ret_ty = tcx.fn_sig(main_id).output();
102     let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
103     let start_instance = ty::Instance::resolve(
104         ecx.tcx.tcx,
105         ty::ParamEnv::reveal_all(),
106         start_id,
107         ecx.tcx.mk_substs(
108             ::std::iter::once(ty::subst::Kind::from(main_ret_ty)))
109         ).unwrap();
110     let start_mir = ecx.load_mir(start_instance.def)?;
111
112     if start_mir.arg_count != 3 {
113         return err!(AbiViolation(format!(
114             "'start' lang item should have three arguments, but has {}",
115             start_mir.arg_count
116         )));
117     }
118
119     // Return value (in static memory so that it does not count as leak).
120     let ret = ecx.layout_of(start_mir.return_ty())?;
121     let ret_ptr = ecx.allocate(ret, MiriMemoryKind::Static.into());
122
123     // Push our stack frame.
124     ecx.push_stack_frame(
125         start_instance,
126         // There is no call site.
127         DUMMY_SP,
128         start_mir,
129         Some(ret_ptr.into()),
130         StackPopCleanup::None { cleanup: true },
131     )?;
132
133     let mut args = ecx.frame().body.args_iter();
134
135     // First argument: pointer to `main()`.
136     let main_ptr = ecx.memory_mut().create_fn_alloc(main_instance);
137     let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
138     ecx.write_scalar(Scalar::Ptr(main_ptr), dest)?;
139
140     // Second argument (argc): `1`.
141     let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
142     let argc = Scalar::from_uint(config.args.len() as u128, dest.layout.size);
143     ecx.write_scalar(argc, dest)?;
144     // Store argc for macOS's `_NSGetArgc`.
145     {
146         let argc_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
147         ecx.write_scalar(argc, argc_place.into())?;
148         ecx.machine.argc = Some(argc_place.ptr.to_ptr()?);
149     }
150
151     // FIXME: extract main source file path.
152     // Third argument (`argv`): created from `config.args`.
153     let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
154     // For Windows, construct a command string with all the aguments.
155     let mut cmd = String::new();
156     for arg in config.args.iter() {
157         if !cmd.is_empty() {
158             cmd.push(' ');
159         }
160         cmd.push_str(&*shell_escape::windows::escape(arg.as_str().into()));
161     }
162     // Don't forget `0` terminator.
163     cmd.push(std::char::from_u32(0).unwrap());
164     // Collect the pointers to the individual strings.
165     let mut argvs = Vec::<Pointer<Tag>>::new();
166     for arg in config.args {
167         // Add `0` terminator.
168         let mut arg = arg.into_bytes();
169         arg.push(0);
170         argvs.push(ecx.memory_mut().allocate_static_bytes(arg.as_slice(), MiriMemoryKind::Static.into()));
171     }
172     // Make an array with all these pointers, in the Miri memory.
173     let argvs_layout = ecx.layout_of(ecx.tcx.mk_array(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8), argvs.len() as u64))?;
174     let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Env.into());
175     for (idx, arg) in argvs.into_iter().enumerate() {
176         let place = ecx.mplace_field(argvs_place, idx as u64)?;
177         ecx.write_scalar(Scalar::Ptr(arg), place.into())?;
178     }
179     ecx.memory_mut().mark_immutable(argvs_place.to_ptr()?.alloc_id)?;
180     // Write a pointer to that place as the argument.
181     let argv = argvs_place.ptr;
182     ecx.write_scalar(argv, dest)?;
183     // Store `argv` for macOS `_NSGetArgv`.
184     {
185         let argv_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
186         ecx.write_scalar(argv, argv_place.into())?;
187         ecx.machine.argv = Some(argv_place.ptr.to_ptr()?);
188     }
189     // Store command line as UTF-16 for Windows `GetCommandLineW`.
190     {
191         let tcx = &{ecx.tcx.tcx};
192         let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
193         let cmd_ptr = ecx.memory_mut().allocate(
194             Size::from_bytes(cmd_utf16.len() as u64 * 2),
195             Align::from_bytes(2).unwrap(),
196             MiriMemoryKind::Env.into(),
197         );
198         ecx.machine.cmd_line = Some(cmd_ptr);
199         // Store the UTF-16 string.
200         let char_size = Size::from_bytes(2);
201         let cmd_alloc = ecx.memory_mut().get_mut(cmd_ptr.alloc_id)?;
202         let mut cur_ptr = cmd_ptr;
203         for &c in cmd_utf16.iter() {
204             cmd_alloc.write_scalar(
205                 tcx,
206                 cur_ptr,
207                 Scalar::from_uint(c, char_size).into(),
208                 char_size,
209             )?;
210             cur_ptr = cur_ptr.offset(char_size, tcx)?;
211         }
212     }
213  
214     assert!(args.next().is_none(), "start lang item has more arguments than expected");
215
216     Ok(ecx)
217 }
218
219 pub fn eval_main<'tcx>(
220     tcx: TyCtxt<'tcx>,
221     main_id: DefId,
222     config: MiriConfig,
223 ) {
224     let mut ecx = match create_ecx(tcx, main_id, config) {
225         Ok(ecx) => ecx,
226         Err(mut err) => {
227             err.print_backtrace();
228             panic!("Miri initialziation error: {}", err.kind)
229         }
230     };
231
232     // Perform the main execution.
233     let res: InterpResult = (|| {
234         ecx.run()?;
235         ecx.run_tls_dtors()
236     })();
237
238     // Process the result.
239     match res {
240         Ok(()) => {
241             let leaks = ecx.memory().leak_report();
242             // Disable the leak test on some platforms where we do not
243             // correctly implement TLS destructors.
244             let target_os = ecx.tcx.tcx.sess.target.target.target_os.to_lowercase();
245             let ignore_leaks = target_os == "windows" || target_os == "macos";
246             if !ignore_leaks && leaks != 0 {
247                 tcx.sess.err("the evaluated program leaked memory");
248             }
249         }
250         Err(mut e) => {
251             // Special treatment for some error kinds
252             let msg = match e.kind {
253                 InterpError::Exit(code) => std::process::exit(code),
254                 InterpError::NoMirFor(..) =>
255                     format!("{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.", e),
256                 _ => e.to_string()
257             };
258             e.print_backtrace();
259             if let Some(frame) = ecx.stack().last() {
260                 let block = &frame.body.basic_blocks()[frame.block];
261                 let span = if frame.stmt < block.statements.len() {
262                     block.statements[frame.stmt].source_info.span
263                 } else {
264                     block.terminator().source_info.span
265                 };
266
267                 let msg = format!("Miri evaluation error: {}", msg);
268                 let mut err = struct_error(ecx.tcx.tcx.at(span), msg.as_str());
269                 let frames = ecx.generate_stacktrace(None);
270                 err.span_label(span, msg);
271                 // We iterate with indices because we need to look at the next frame (the caller).
272                 for idx in 0..frames.len() {
273                     let frame_info = &frames[idx];
274                     let call_site_is_local = frames.get(idx+1).map_or(false,
275                         |caller_info| caller_info.instance.def_id().is_local());
276                     if call_site_is_local {
277                         err.span_note(frame_info.call_site, &frame_info.to_string());
278                     } else {
279                         err.note(&frame_info.to_string());
280                     }
281                 }
282                 err.emit();
283             } else {
284                 ecx.tcx.sess.err(&msg);
285             }
286
287             for (i, frame) in ecx.stack().iter().enumerate() {
288                 trace!("-------------------");
289                 trace!("Frame {}", i);
290                 trace!("    return: {:?}", frame.return_place.map(|p| *p));
291                 for (i, local) in frame.locals.iter().enumerate() {
292                     trace!("    local {}: {:?}", i, local.value);
293                 }
294             }
295         }
296     }
297 }
298
299 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
300 pub enum MiriMemoryKind {
301     /// `__rust_alloc` memory.
302     Rust,
303     /// `malloc` memory.
304     C,
305     /// Part of env var emulation.
306     Env,
307     /// Statics.
308     Static,
309 }
310
311 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
312     #[inline(always)]
313     fn into(self) -> MemoryKind<MiriMemoryKind> {
314         MemoryKind::Machine(self)
315     }
316 }
317
318 impl MayLeak for MiriMemoryKind {
319     #[inline(always)]
320     fn may_leak(self) -> bool {
321         use self::MiriMemoryKind::*;
322         match self {
323             Rust | C => false,
324             Env | Static => true,
325         }
326     }
327 }
328
329 pub struct Evaluator<'tcx> {
330     /// Environment variables set by `setenv`.
331     /// Miri does not expose env vars from the host to the emulated program.
332     pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Tag>>,
333
334     /// Program arguments (`Option` because we can only initialize them after creating the ecx).
335     /// These are *pointers* to argc/argv because macOS.
336     /// We also need the full command line as one string because of Windows.
337     pub(crate) argc: Option<Pointer<Tag>>,
338     pub(crate) argv: Option<Pointer<Tag>>,
339     pub(crate) cmd_line: Option<Pointer<Tag>>,
340
341     /// Last OS error.
342     pub(crate) last_error: u32,
343
344     /// TLS state.
345     pub(crate) tls: TlsData<'tcx>,
346
347     /// Whether to enforce the validity invariant.
348     pub(crate) validate: bool,
349 }
350
351 impl<'tcx> Evaluator<'tcx> {
352     fn new(validate: bool) -> Self {
353         Evaluator {
354             env_vars: HashMap::default(),
355             argc: None,
356             argv: None,
357             cmd_line: None,
358             last_error: 0,
359             tls: TlsData::default(),
360             validate,
361         }
362     }
363 }
364
365 // FIXME: rustc issue <https://github.com/rust-lang/rust/issues/47131>.
366 #[allow(dead_code)]
367 type MiriEvalContext<'mir, 'tcx> = InterpretCx<'mir, 'tcx, Evaluator<'tcx>>;
368
369 // A little trait that's useful to be inherited by extension traits.
370 pub trait MiriEvalContextExt<'mir, 'tcx> {
371     fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx>;
372     fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx>;
373 }
374 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
375     #[inline(always)]
376     fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
377         self
378     }
379     #[inline(always)]
380     fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
381         self
382     }
383 }
384
385 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
386     type MemoryKinds = MiriMemoryKind;
387
388     type FrameExtra = stacked_borrows::CallId;
389     type MemoryExtra = memory::MemoryExtra;
390     type AllocExtra = memory::AllocExtra;
391     type PointerTag = Tag;
392
393     type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
394
395     const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Static);
396
397     #[inline(always)]
398     fn enforce_validity(ecx: &InterpretCx<'mir, 'tcx, Self>) -> bool {
399         ecx.machine.validate
400     }
401
402     /// Returns `Ok()` when the function was handled; fail otherwise.
403     #[inline(always)]
404     fn find_fn(
405         ecx: &mut InterpretCx<'mir, 'tcx, Self>,
406         instance: ty::Instance<'tcx>,
407         args: &[OpTy<'tcx, Tag>],
408         dest: Option<PlaceTy<'tcx, Tag>>,
409         ret: Option<mir::BasicBlock>,
410     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
411         ecx.find_fn(instance, args, dest, ret)
412     }
413
414     #[inline(always)]
415     fn call_intrinsic(
416         ecx: &mut rustc_mir::interpret::InterpretCx<'mir, 'tcx, Self>,
417         instance: ty::Instance<'tcx>,
418         args: &[OpTy<'tcx, Tag>],
419         dest: PlaceTy<'tcx, Tag>,
420     ) -> InterpResult<'tcx> {
421         ecx.call_intrinsic(instance, args, dest)
422     }
423
424     #[inline(always)]
425     fn ptr_op(
426         ecx: &rustc_mir::interpret::InterpretCx<'mir, 'tcx, Self>,
427         bin_op: mir::BinOp,
428         left: ImmTy<'tcx, Tag>,
429         right: ImmTy<'tcx, Tag>,
430     ) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
431         ecx.ptr_op(bin_op, left, right)
432     }
433
434     fn box_alloc(
435         ecx: &mut InterpretCx<'mir, 'tcx, Self>,
436         dest: PlaceTy<'tcx, Tag>,
437     ) -> InterpResult<'tcx> {
438         trace!("box_alloc for {:?}", dest.layout.ty);
439         // Call the `exchange_malloc` lang item.
440         let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
441         let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
442         let malloc_mir = ecx.load_mir(malloc.def)?;
443         ecx.push_stack_frame(
444             malloc,
445             malloc_mir.span,
446             malloc_mir,
447             Some(dest),
448             // Don't do anything when we are done. The `statement()` function will increment
449             // the old stack frame's stmt counter to the next statement, which means that when
450             // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
451             StackPopCleanup::None { cleanup: true },
452         )?;
453
454         let mut args = ecx.frame().body.args_iter();
455         let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
456
457         // First argument: `size`.
458         // (`0` is allowed here -- this is expected to be handled by the lang item).
459         let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
460         let size = layout.size.bytes();
461         ecx.write_scalar(Scalar::from_uint(size, arg.layout.size), arg)?;
462
463         // Second argument: `align`.
464         let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
465         let align = layout.align.abi.bytes();
466         ecx.write_scalar(Scalar::from_uint(align, arg.layout.size), arg)?;
467
468         // No more arguments.
469         assert!(
470             args.next().is_none(),
471             "`exchange_malloc` lang item has more arguments than expected"
472         );
473         Ok(())
474     }
475
476     fn find_foreign_static(
477         def_id: DefId,
478         tcx: TyCtxtAt<'tcx>,
479     ) -> InterpResult<'tcx, Cow<'tcx, Allocation>> {
480         let attrs = tcx.get_attrs(def_id);
481         let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
482             Some(name) => name.as_str(),
483             None => tcx.item_name(def_id).as_str(),
484         };
485
486         let alloc = match link_name.get() {
487             "__cxa_thread_atexit_impl" => {
488                 // This should be all-zero, pointer-sized.
489                 let size = tcx.data_layout.pointer_size;
490                 let data = vec![0; size.bytes() as usize];
491                 Allocation::from_bytes(&data, tcx.data_layout.pointer_align.abi)
492             }
493             _ => return err!(Unimplemented(
494                     format!("can't access foreign static: {}", link_name),
495                 )),
496         };
497         Ok(Cow::Owned(alloc))
498     }
499
500     #[inline(always)]
501     fn before_terminator(_ecx: &mut InterpretCx<'mir, 'tcx, Self>) -> InterpResult<'tcx>
502     {
503         // We are not interested in detecting loops.
504         Ok(())
505     }
506
507     fn tag_allocation<'b>(
508         id: AllocId,
509         alloc: Cow<'b, Allocation>,
510         kind: Option<MemoryKind<Self::MemoryKinds>>,
511         memory: &Memory<'mir, 'tcx, Self>,
512     ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
513         let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
514         let alloc = alloc.into_owned();
515         let (stacks, base_tag) = Stacks::new_allocation(
516             id,
517             Size::from_bytes(alloc.bytes.len() as u64),
518             Rc::clone(&memory.extra.stacked_borrows),
519             kind,
520         );
521         if kind != MiriMemoryKind::Static.into() {
522             assert!(alloc.relocations.is_empty(), "Only statics can come initialized with inner pointers");
523             // Now we can rely on the inner pointers being static, too.
524         }
525         let mut memory_extra = memory.extra.stacked_borrows.borrow_mut();
526         let alloc: Allocation<Tag, Self::AllocExtra> = Allocation {
527             bytes: alloc.bytes,
528             relocations: Relocations::from_presorted(
529                 alloc.relocations.iter()
530                     // The allocations in the relocations (pointers stored *inside* this allocation)
531                     // all get the base pointer tag.
532                     .map(|&(offset, ((), alloc))| (offset, (memory_extra.static_base_ptr(alloc), alloc)))
533                     .collect()
534             ),
535             undef_mask: alloc.undef_mask,
536             align: alloc.align,
537             mutability: alloc.mutability,
538             extra: AllocExtra {
539                 stacked_borrows: stacks,
540                 intptrcast: Default::default(),
541             },
542         };
543         (Cow::Owned(alloc), base_tag)
544     }
545
546     #[inline(always)]
547     fn tag_static_base_pointer(
548         id: AllocId,
549         memory: &Memory<'mir, 'tcx, Self>,
550     ) -> Self::PointerTag {
551         memory.extra.stacked_borrows.borrow_mut().static_base_ptr(id)
552     }
553
554     #[inline(always)]
555     fn retag(
556         ecx: &mut InterpretCx<'mir, 'tcx, Self>,
557         kind: mir::RetagKind,
558         place: PlaceTy<'tcx, Tag>,
559     ) -> InterpResult<'tcx> {
560         if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag || !Self::enforce_validity(ecx) {
561             // No tracking, or no retagging. The latter is possible because a dependency of ours
562             // might be called with different flags than we are, so there are `Retag`
563             // statements but we do not want to execute them.
564             // Also, honor the whitelist in `enforce_validity` because otherwise we might retag
565             // uninitialized data.
566              Ok(())
567         } else {
568             ecx.retag(kind, place)
569         }
570     }
571
572     #[inline(always)]
573     fn stack_push(
574         ecx: &mut InterpretCx<'mir, 'tcx, Self>,
575     ) -> InterpResult<'tcx, stacked_borrows::CallId> {
576         Ok(ecx.memory().extra.stacked_borrows.borrow_mut().new_call())
577     }
578
579     #[inline(always)]
580     fn stack_pop(
581         ecx: &mut InterpretCx<'mir, 'tcx, Self>,
582         extra: stacked_borrows::CallId,
583     ) -> InterpResult<'tcx> {
584         Ok(ecx.memory().extra.stacked_borrows.borrow_mut().end_call(extra))
585     }
586
587     fn int_to_ptr(
588         int: u64,
589         memory: &Memory<'mir, 'tcx, Self>,
590     ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
591         if int == 0 {
592             err!(InvalidNullPointerUsage)
593         } else if memory.extra.rng.is_none() {
594             err!(ReadBytesAsPointer)
595         } else {
596            intptrcast::GlobalState::int_to_ptr(int, memory)
597         }
598     }
599  
600     fn ptr_to_int(
601         ptr: Pointer<Self::PointerTag>,
602         memory: &Memory<'mir, 'tcx, Self>,
603     ) -> InterpResult<'tcx, u64> {
604         if memory.extra.rng.is_none() {
605             err!(ReadPointerAsBytes)
606         } else {
607             intptrcast::GlobalState::ptr_to_int(ptr, memory)
608         }
609     }
610 }