Skip to content

Commit 5d9e2c8

Browse files
committed
Bound the size of created allocations.
1 parent 18e6b5a commit 5d9e2c8

9 files changed

+413
-189
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -556,20 +556,26 @@ impl<'tcx, 'locals> Collector<'tcx, 'locals> {
556556
let ty = place.ty(self.local_decls, self.patch.tcx).ty;
557557
let place = map.find(place.as_ref())?;
558558
let layout = ecx.layout_of(ty).ok()?;
559-
if layout.abi.is_scalar() {
560-
let value = propagatable_scalar(*ecx.tcx, place, state, map)?;
559+
if layout.is_zst() {
560+
Some(ConstantKind::Val(ConstValue::ZeroSized, ty))
561+
} else if layout.abi.is_scalar()
562+
&& let Some(value) = propagatable_scalar(*ecx.tcx, place, state, map)
563+
{
561564
Some(ConstantKind::Val(ConstValue::Scalar(value), ty))
562-
} else {
565+
} else if layout.is_sized() && layout.size <= 4 * ecx.tcx.data_layout.pointer_size {
563566
let alloc_id = ecx
564567
.intern_with_temp_alloc(layout, |ecx, dest| {
565568
try_write_constant(ecx, dest, place, ty, state, map)
566569
})
567570
.ok()?;
568571
Some(ConstantKind::Val(ConstValue::Indirect { alloc_id, offset: Size::ZERO }, ty))
572+
} else {
573+
None
569574
}
570575
}
571576
}
572577

578+
#[instrument(level = "trace", skip(tcx, state, map), ret)]
573579
fn propagatable_scalar<'tcx>(
574580
tcx: TyCtxt<'tcx>,
575581
place: PlaceIndex,
@@ -602,17 +608,25 @@ fn try_write_constant<'tcx>(
602608
map: &Map,
603609
) -> InterpResult<'tcx> {
604610
let layout = ecx.layout_of(ty)?;
611+
612+
// Fast path for ZSTs.
613+
if layout.is_zst() {
614+
return Ok(());
615+
}
616+
617+
// Fast path for scalars.
618+
if layout.abi.is_scalar()
619+
&& let Some(value) = propagatable_scalar(*ecx.tcx, place, state, map)
620+
{
621+
return ecx.write_immediate(Immediate::Scalar(value), dest);
622+
}
623+
605624
match ty.kind() {
606625
// ZSTs. Nothing to do.
607626
ty::FnDef(..) => {}
608627

609-
// Scalars.
610-
_ if layout.abi.is_scalar() => {
611-
let value = propagatable_scalar(*ecx.tcx, place, state, map).ok_or(err_inval!(ConstPropNonsense))?;
612-
ecx.write_immediate(Immediate::Scalar(value), dest)?;
613-
}
614628
// Those are scalars, must be handled above.
615-
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => bug!(),
629+
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => throw_inval!(ConstPropNonsense),
616630

617631
ty::Tuple(elem_tys) => {
618632
for (i, elem) in elem_tys.iter().enumerate() {

tests/mir-opt/dataflow-const-prop/enum.statics.DataflowConstProp.32bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116

117117
- alloc2 (static: RC, size: 4, align: 4) {
118118
- ╾─alloc14─╼ │ ╾──╼
119-
+ alloc20 (size: 8, align: 4) {
119+
+ alloc28 (size: 8, align: 4) {
120120
+ 00 00 00 00 00 00 00 00 │ ........
121121
}
122122

tests/mir-opt/dataflow-const-prop/enum.statics.DataflowConstProp.64bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116

117117
- alloc2 (static: RC, size: 8, align: 8) {
118118
- ╾───────alloc14───────╼ │ ╾──────╼
119-
+ alloc20 (size: 8, align: 4) {
119+
+ alloc28 (size: 8, align: 4) {
120120
+ 00 00 00 00 00 00 00 00 │ ........
121121
}
122122

tests/mir-opt/dataflow-const-prop/repr_transparent.main.DataflowConstProp.diff

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
bb0: {
1919
StorageLive(_1);
20-
_1 = I32(const 0_i32);
20+
- _1 = I32(const 0_i32);
21+
+ _1 = const I32(0_i32);
2122
StorageLive(_2);
2223
StorageLive(_3);
2324
StorageLive(_4);
@@ -31,12 +32,20 @@
3132
StorageDead(_5);
3233
StorageDead(_4);
3334
- _2 = I32(move _3);
34-
+ _2 = I32(const 0_i32);
35+
+ _2 = const I32(0_i32);
3536
StorageDead(_3);
3637
_0 = const ();
3738
StorageDead(_2);
3839
StorageDead(_1);
3940
return;
4041
}
42+
+ }
43+
+
44+
+ alloc5 (size: 4, align: 4) {
45+
+ 00 00 00 00 │ ....
46+
+ }
47+
+
48+
+ alloc4 (size: 4, align: 4) {
49+
+ 00 00 00 00 │ ....
4150
}
4251

0 commit comments

Comments
 (0)