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