Skip to content

Commit 8f1ea57

Browse files
committed
only allocate bytes within AllocRange
1 parent c41339a commit 8f1ea57

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
1010
use crate::rustc_internal::{self, opaque};
1111
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
12-
use crate::stable_mir::ty::{new_allocation, FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy};
12+
use crate::stable_mir::ty::{
13+
allocation_filter, new_allocation, FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy,
14+
};
1315
use crate::stable_mir::{self, Context};
1416
use rustc_hir as hir;
1517
use rustc_middle::mir::coverage::CodeRegion;
18+
use rustc_middle::mir::interpret::alloc_range;
1619
use rustc_middle::mir::{self, ConstantKind};
1720
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
1821
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -1080,30 +1083,7 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
10801083
type T = stable_mir::ty::Allocation;
10811084

10821085
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1083-
let size = self.size();
1084-
let mut bytes: Vec<Option<u8>> = self
1085-
.inspect_with_uninit_and_ptr_outside_interpreter(0..size.bytes_usize())
1086-
.iter()
1087-
.copied()
1088-
.map(Some)
1089-
.collect();
1090-
for (i, b) in bytes.iter_mut().enumerate() {
1091-
if !self.init_mask().get(rustc_target::abi::Size::from_bytes(i)) {
1092-
*b = None;
1093-
}
1094-
}
1095-
stable_mir::ty::Allocation {
1096-
bytes: bytes,
1097-
provenance: {
1098-
let mut ptrs = Vec::new();
1099-
for (size, prov) in self.provenance().ptrs().iter() {
1100-
ptrs.push((size.bytes_usize(), opaque(prov)));
1101-
}
1102-
stable_mir::ty::ProvenanceMap { ptrs }
1103-
},
1104-
align: self.align.bytes(),
1105-
mutability: self.mutability.stable(tables),
1106-
}
1086+
allocation_filter(self, alloc_range(rustc_target::abi::Size::ZERO, self.size()), tables)
11071087
}
11081088
}
11091089

compiler/rustc_smir/src/stable_mir/ty.rs

+42-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use rustc_middle::mir::interpret::{alloc_range, ConstValue, Pointer};
1+
use rustc_middle::mir::interpret::{alloc_range, AllocRange, ConstValue, Pointer};
22

33
use super::{mir::Mutability, mir::Safety, with, DefId};
44
use crate::{
5-
rustc_internal::Opaque,
5+
rustc_internal::{opaque, Opaque},
66
rustc_smir::{Stable, Tables},
77
};
88

@@ -353,17 +353,50 @@ pub fn new_allocation<'tcx>(
353353
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty()))
354354
.unwrap()
355355
.size;
356-
let bytes = alloc.0.get_bytes_unchecked(alloc_range(offset, ty_size));
357-
let offset_allocation = rustc_middle::mir::interpret::Allocation::from_bytes(
358-
bytes,
359-
alloc.0.align,
360-
alloc.0.mutability,
361-
);
362-
offset_allocation.stable(tables)
356+
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)
363357
}
364358
}
365359
}
366360

361+
/// Creates an `Allocation` only from information within the `AllocRange`.
362+
pub fn allocation_filter<'tcx>(
363+
alloc: &rustc_middle::mir::interpret::Allocation,
364+
alloc_range: AllocRange,
365+
tables: &mut Tables<'tcx>,
366+
) -> Allocation {
367+
let mut bytes: Vec<Option<u8>> = alloc
368+
.inspect_with_uninit_and_ptr_outside_interpreter(
369+
alloc_range.start.bytes_usize()..alloc_range.end().bytes_usize(),
370+
)
371+
.iter()
372+
.copied()
373+
.map(Some)
374+
.collect();
375+
for (i, b) in bytes.iter_mut().enumerate() {
376+
if !alloc
377+
.init_mask()
378+
.get(rustc_target::abi::Size::from_bytes(i + alloc_range.start.bytes_usize()))
379+
{
380+
*b = None;
381+
}
382+
}
383+
let mut ptrs = Vec::new();
384+
for (offset, prov) in alloc
385+
.provenance()
386+
.ptrs()
387+
.iter()
388+
.filter(|a| a.0 >= alloc_range.start && a.0 <= alloc_range.end())
389+
{
390+
ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), opaque(prov)));
391+
}
392+
Allocation {
393+
bytes: bytes,
394+
provenance: ProvenanceMap { ptrs },
395+
align: alloc.align.bytes(),
396+
mutability: alloc.mutability.stable(tables),
397+
}
398+
}
399+
367400
#[derive(Clone, Debug)]
368401
pub enum ConstantKind {
369402
Allocated(Allocation),

0 commit comments

Comments
 (0)