Skip to content

Commit 4955d23

Browse files
enum instead of flag
1 parent e685a36 commit 4955d23

File tree

14 files changed

+71
-51
lines changed

14 files changed

+71
-51
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ use super::error::*;
2121
use crate::errors::{LongRunning, LongRunningWarn};
2222
use crate::fluent_generated as fluent;
2323
use crate::interpret::{
24-
self, AllocId, AllocRange, ConstAllocation, CtfeProvenance, FnArg, Frame, GlobalAlloc, ImmTy,
25-
InterpCx, InterpResult, MPlaceTy, OpTy, RangeSet, Scalar, compile_time_machine, interp_ok,
26-
throw_exhaust, throw_inval, throw_ub, throw_ub_custom, throw_unsup, throw_unsup_format,
24+
self, AllocId, AllocInit, AllocRange, ConstAllocation, CtfeProvenance, FnArg, Frame,
25+
GlobalAlloc, ImmTy, InterpCx, InterpResult, MPlaceTy, OpTy, RangeSet, Scalar,
26+
compile_time_machine, interp_ok, throw_exhaust, throw_inval, throw_ub, throw_ub_custom,
27+
throw_unsup, throw_unsup_format,
2728
};
2829

2930
/// When hitting this many interpreted terminators we emit a deny by default lint
@@ -420,7 +421,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
420421
Size::from_bytes(size),
421422
align,
422423
interpret::MemoryKind::Machine(MemoryKind::Heap),
423-
false,
424+
AllocInit::Uninit,
424425
)?;
425426
ecx.write_pointer(ptr, dest)?;
426427
}

compiler/rustc_const_eval/src/interpret/memory.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2020
use tracing::{debug, instrument, trace};
2121

2222
use super::{
23-
AllocBytes, AllocId, AllocMap, AllocRange, Allocation, CheckAlignMsg, CheckInAllocMsg,
24-
CtfeProvenance, GlobalAlloc, InterpCx, InterpResult, Machine, MayLeak, Misalignment, Pointer,
25-
PointerArithmetic, Provenance, Scalar, alloc_range, err_ub, err_ub_custom, interp_ok, throw_ub,
26-
throw_ub_custom, throw_unsup, throw_unsup_format,
23+
AllocBytes, AllocId, AllocInit, AllocMap, AllocRange, Allocation, CheckAlignMsg,
24+
CheckInAllocMsg, CtfeProvenance, GlobalAlloc, InterpCx, InterpResult, Machine, MayLeak,
25+
Misalignment, Pointer, PointerArithmetic, Provenance, Scalar, alloc_range, err_ub,
26+
err_ub_custom, interp_ok, throw_ub, throw_ub_custom, throw_unsup, throw_unsup_format,
2727
};
2828
use crate::fluent_generated as fluent;
2929

@@ -230,12 +230,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
230230
size: Size,
231231
align: Align,
232232
kind: MemoryKind<M::MemoryKind>,
233-
zero_init: bool,
233+
init: AllocInit,
234234
) -> InterpResult<'tcx, Pointer<M::Provenance>> {
235235
let alloc = if M::PANIC_ON_ALLOC_FAIL {
236-
Allocation::new(size, align, zero_init)
236+
Allocation::new(size, align, init)
237237
} else {
238-
Allocation::try_new(size, align, zero_init)?
238+
Allocation::try_new(size, align, init)?
239239
};
240240
self.insert_allocation(alloc, kind)
241241
}
@@ -278,7 +278,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
278278
new_size: Size,
279279
new_align: Align,
280280
kind: MemoryKind<M::MemoryKind>,
281-
zero_init: bool,
281+
init: AllocInit,
282282
) -> InterpResult<'tcx, Pointer<M::Provenance>> {
283283
let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
284284
if offset.bytes() != 0 {
@@ -291,7 +291,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
291291

292292
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
293293
// This happens so rarely, the perf advantage is outweighed by the maintenance cost.
294-
let new_ptr = self.allocate_ptr(new_size, new_align, kind, zero_init)?;
294+
let new_ptr = self.allocate_ptr(new_size, new_align, kind, init)?;
295295
let old_size = match old_size_and_align {
296296
Some((size, _align)) => size,
297297
None => self.get_alloc_raw(alloc_id)?.size(),

compiler/rustc_const_eval/src/interpret/place.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use rustc_middle::{bug, mir, span_bug};
1212
use tracing::{instrument, trace};
1313

1414
use super::{
15-
AllocRef, AllocRefMut, CheckAlignMsg, CtfeProvenance, ImmTy, Immediate, InterpCx, InterpResult,
16-
Machine, MemoryKind, Misalignment, OffsetMode, OpTy, Operand, Pointer, Projectable, Provenance,
17-
Scalar, alloc_range, interp_ok, mir_assign_valid_types,
15+
AllocInit, AllocRef, AllocRefMut, CheckAlignMsg, CtfeProvenance, ImmTy, Immediate, InterpCx,
16+
InterpResult, Machine, MemoryKind, Misalignment, OffsetMode, OpTy, Operand, Pointer,
17+
Projectable, Provenance, Scalar, alloc_range, interp_ok, mir_assign_valid_types,
1818
};
1919

2020
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
@@ -983,7 +983,7 @@ where
983983
let Some((size, align)) = self.size_and_align_of(&meta, &layout)? else {
984984
span_bug!(self.cur_span(), "cannot allocate space for `extern` type, size is not known")
985985
};
986-
let ptr = self.allocate_ptr(size, align, kind, false)?;
986+
let ptr = self.allocate_ptr(size, align, kind, AllocInit::Uninit)?;
987987
interp_ok(self.ptr_with_meta_to_mplace(ptr.into(), meta, layout, /*unaligned*/ false))
988988
}
989989

compiler/rustc_const_eval/src/interpret/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ops::ControlFlow;
22

33
use rustc_hir::def_id::LocalDefId;
44
use rustc_middle::mir;
5-
use rustc_middle::mir::interpret::{Allocation, InterpResult, Pointer};
5+
use rustc_middle::mir::interpret::{AllocInit, Allocation, InterpResult, Pointer};
66
use rustc_middle::ty::layout::TyAndLayout;
77
use rustc_middle::ty::{
88
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
@@ -76,7 +76,7 @@ pub(crate) fn create_static_alloc<'tcx>(
7676
static_def_id: LocalDefId,
7777
layout: TyAndLayout<'tcx>,
7878
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
79-
let alloc = Allocation::try_new(layout.size, layout.align.abi, false)?;
79+
let alloc = Allocation::try_new(layout.size, layout.align.abi, AllocInit::Uninit)?;
8080
let alloc_id = ecx.tcx.reserve_and_set_static_alloc(static_def_id.into());
8181
assert_eq!(ecx.machine.static_root_ids, None);
8282
ecx.machine.static_root_ids = Some((alloc_id, static_def_id));

compiler/rustc_middle/src/mir/interpret/allocation.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ impl AllocRange {
270270
}
271271
}
272272

273+
/// Whether a new allocation should be initialized with zero-bytes.
274+
pub enum AllocInit {
275+
Uninit,
276+
Zero,
277+
}
278+
273279
// The constructors are all without extra; the extra gets added by a machine hook later.
274280
impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
275281
/// Creates an allocation initialized by the given bytes
@@ -297,7 +303,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
297303
fn new_inner<R>(
298304
size: Size,
299305
align: Align,
300-
zero_init: bool,
306+
init: AllocInit,
301307
fail: impl FnOnce() -> R,
302308
) -> Result<Self, R> {
303309
// We raise an error if we cannot create the allocation on the host.
@@ -311,7 +317,10 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
311317
Ok(Allocation {
312318
bytes,
313319
provenance: ProvenanceMap::new(),
314-
init_mask: InitMask::new(size, zero_init),
320+
init_mask: InitMask::new(size, match init {
321+
AllocInit::Uninit => false,
322+
AllocInit::Zero => true,
323+
}),
315324
align,
316325
mutability: Mutability::Mut,
317326
extra: (),
@@ -320,8 +329,8 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
320329

321330
/// Try to create an Allocation of `size` bytes, failing if there is not enough memory
322331
/// available to the compiler to do so.
323-
pub fn try_new<'tcx>(size: Size, align: Align, zero_init: bool) -> InterpResult<'tcx, Self> {
324-
Self::new_inner(size, align, zero_init, || {
332+
pub fn try_new<'tcx>(size: Size, align: Align, init: AllocInit) -> InterpResult<'tcx, Self> {
333+
Self::new_inner(size, align, init, || {
325334
ty::tls::with(|tcx| tcx.dcx().delayed_bug("exhausted memory during interpretation"));
326335
InterpErrorKind::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
327336
})
@@ -333,8 +342,8 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
333342
///
334343
/// Example use case: To obtain an Allocation filled with specific data,
335344
/// first call this function and then call write_scalar to fill in the right data.
336-
pub fn new(size: Size, align: Align, zero_init: bool) -> Self {
337-
match Self::new_inner(size, align, zero_init, || {
345+
pub fn new(size: Size, align: Align, init: AllocInit) -> Self {
346+
match Self::new_inner(size, align, init, || {
338347
panic!(
339348
"interpreter ran out of memory: cannot create allocation of {} bytes",
340349
size.bytes()

compiler/rustc_middle/src/mir/interpret/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub use {
3030
};
3131

3232
pub use self::allocation::{
33-
AllocBytes, AllocError, AllocRange, AllocResult, Allocation, ConstAllocation, InitChunk,
34-
InitChunkIter, alloc_range,
33+
AllocBytes, AllocError, AllocInit, AllocRange, AllocResult, Allocation, ConstAllocation,
34+
InitChunk, InitChunkIter, alloc_range,
3535
};
3636
pub use self::error::{
3737
BadBytesAccess, CheckAlignMsg, CheckInAllocMsg, ErrorHandled, EvalStaticInitializerRawResult,

compiler/rustc_middle/src/ty/vtable.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use rustc_ast::Mutability;
44
use rustc_macros::HashStable;
55
use rustc_type_ir::elaborate;
66

7-
use crate::mir::interpret::{AllocId, Allocation, CTFE_ALLOC_SALT, Pointer, Scalar, alloc_range};
7+
use crate::mir::interpret::{
8+
AllocId, AllocInit, Allocation, CTFE_ALLOC_SALT, Pointer, Scalar, alloc_range,
9+
};
810
use crate::ty::{self, Instance, PolyTraitRef, Ty, TyCtxt};
911

1012
#[derive(Clone, Copy, PartialEq, HashStable)]
@@ -108,7 +110,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
108110
let ptr_align = tcx.data_layout.pointer_align.abi;
109111

110112
let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap();
111-
let mut vtable = Allocation::new(vtable_size, ptr_align, false);
113+
let mut vtable = Allocation::new(vtable_size, ptr_align, AllocInit::Uninit);
112114

113115
// No need to do any alignment checks on the memory accesses below, because we know the
114116
// allocation is correctly aligned as we created it above. Also we're only offsetting by

compiler/rustc_smir/src/rustc_smir/alloc.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_abi::{Align, Size};
22
use rustc_middle::mir::ConstValue;
3-
use rustc_middle::mir::interpret::{AllocRange, Pointer, alloc_range};
3+
use rustc_middle::mir::interpret::{AllocInit, AllocRange, Pointer, alloc_range};
44
use stable_mir::Error;
55
use stable_mir::mir::Mutability;
66
use stable_mir::ty::{Allocation, ProvenanceMap};
@@ -45,7 +45,7 @@ pub(crate) fn try_new_allocation<'tcx>(
4545
.map_err(|e| e.stable(tables))?
4646
.align;
4747
let mut allocation =
48-
rustc_middle::mir::interpret::Allocation::new(size, align.abi, false);
48+
rustc_middle::mir::interpret::Allocation::new(size, align.abi, AllocInit::Uninit);
4949
allocation
5050
.write_scalar(&tables.tcx, alloc_range(Size::ZERO, size), scalar)
5151
.map_err(|e| e.stable(tables))?;
@@ -69,8 +69,11 @@ pub(crate) fn try_new_allocation<'tcx>(
6969
.tcx
7070
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
7171
.map_err(|e| e.stable(tables))?;
72-
let mut allocation =
73-
rustc_middle::mir::interpret::Allocation::new(layout.size, layout.align.abi, false);
72+
let mut allocation = rustc_middle::mir::interpret::Allocation::new(
73+
layout.size,
74+
layout.align.abi,
75+
AllocInit::Uninit,
76+
);
7477
allocation
7578
.write_scalar(
7679
&tables.tcx,

src/tools/miri/src/shims/alloc.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
7878
}
7979
}
8080

81-
fn malloc(&mut self, size: u64, zero_init: bool) -> InterpResult<'tcx, Pointer> {
81+
fn malloc(&mut self, size: u64, init: AllocInit) -> InterpResult<'tcx, Pointer> {
8282
let this = self.eval_context_mut();
8383
let align = this.malloc_align(size);
84-
let ptr = this.allocate_ptr(Size::from_bytes(size), align, MiriMemoryKind::C.into(), zero_init)?;
84+
let ptr = this.allocate_ptr(Size::from_bytes(size), align, MiriMemoryKind::C.into(), init)?;
8585
interp_ok(ptr.into())
8686
}
8787

@@ -105,7 +105,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
105105
Size::from_bytes(size),
106106
Align::from_bytes(align).unwrap(),
107107
MiriMemoryKind::C.into(),
108-
false
108+
AllocInit::Uninit
109109
)?;
110110
this.write_pointer(ptr, &memptr)?;
111111
interp_ok(Scalar::from_i32(0))
@@ -125,7 +125,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
125125
let new_align = this.malloc_align(new_size);
126126
if this.ptr_is_null(old_ptr)? {
127127
// Here we must behave like `malloc`.
128-
self.malloc(new_size, /*zero_init*/ false)
128+
self.malloc(new_size, AllocInit::Uninit)
129129
} else {
130130
if new_size == 0 {
131131
// C, in their infinite wisdom, made this UB.
@@ -138,7 +138,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
138138
Size::from_bytes(new_size),
139139
new_align,
140140
MiriMemoryKind::C.into(),
141-
false
141+
AllocInit::Uninit
142142
)?;
143143
interp_ok(new_ptr.into())
144144
}
@@ -179,7 +179,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
179179
Size::from_bytes(size),
180180
Align::from_bytes(align).unwrap(),
181181
MiriMemoryKind::C.into(),
182-
false
182+
AllocInit::Uninit
183183
)?;
184184
interp_ok(ptr.into())
185185
}

src/tools/miri/src/shims/foreign_items.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_ast::expand::allocator::alloc_error_handler_name;
88
use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::CrateNum;
1010
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
11+
use rustc_middle::mir::interpret::AllocInit;
1112
use rustc_middle::ty::Ty;
1213
use rustc_middle::{mir, ty};
1314
use rustc_span::Symbol;
@@ -441,7 +442,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
441442
let [size] = this.check_shim(abi, Conv::C, link_name, args)?;
442443
let size = this.read_target_usize(size)?;
443444
if size <= this.max_size_of_val().bytes() {
444-
let res = this.malloc(size, /*zero_init:*/ false)?;
445+
let res = this.malloc(size, AllocInit::Uninit)?;
445446
this.write_pointer(res, dest)?;
446447
} else {
447448
// If this does not fit in an isize, return null and, on Unix, set errno.
@@ -456,7 +457,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
456457
let items = this.read_target_usize(items)?;
457458
let elem_size = this.read_target_usize(elem_size)?;
458459
if let Some(size) = this.compute_size_in_bytes(Size::from_bytes(elem_size), items) {
459-
let res = this.malloc(size.bytes(), /*zero_init:*/ true)?;
460+
let res = this.malloc(size.bytes(), AllocInit::Zero)?;
460461
this.write_pointer(res, dest)?;
461462
} else {
462463
// On size overflow, return null and, on Unix, set errno.
@@ -508,7 +509,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
508509
Size::from_bytes(size),
509510
Align::from_bytes(align).unwrap(),
510511
memory_kind.into(),
511-
false
512+
AllocInit::Uninit
512513
)?;
513514

514515
ecx.write_pointer(ptr, dest)
@@ -537,7 +538,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
537538
Size::from_bytes(size),
538539
Align::from_bytes(align).unwrap(),
539540
MiriMemoryKind::Rust.into(),
540-
true
541+
AllocInit::Zero
541542
)?;
542543
this.write_pointer(ptr, dest)
543544
});
@@ -598,7 +599,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
598599
Size::from_bytes(new_size),
599600
align,
600601
MiriMemoryKind::Rust.into(),
601-
false
602+
AllocInit::Uninit
602603
)?;
603604
this.write_pointer(new_ptr, dest)
604605
});

src/tools/miri/src/shims/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11091109
Size::from_bytes(size),
11101110
dirent_layout.align.abi,
11111111
MiriMemoryKind::Runtime.into(),
1112-
false
1112+
AllocInit::Uninit
11131113
)?;
11141114
let entry: Pointer = entry.into();
11151115

src/tools/miri/src/shims/unix/linux/mem.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4949
Size::from_bytes(new_size),
5050
align,
5151
MiriMemoryKind::Mmap.into(),
52-
true
52+
AllocInit::Zero
5353
)?;
5454

5555
interp_ok(Scalar::from_pointer(ptr, this))

src/tools/miri/src/shims/unix/mem.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
116116
align,
117117
MiriMemoryKind::Mmap.into(),
118118
// mmap guarantees new mappings are zero-init.
119-
true
119+
AllocInit::Zero
120120
)?;
121121

122122
interp_ok(Scalar::from_pointer(ptr, this))

src/tools/miri/src/shims/windows/foreign_items.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,20 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
253253
this.read_target_isize(handle)?;
254254
let flags = this.read_scalar(flags)?.to_u32()?;
255255
let size = this.read_target_usize(size)?;
256-
let heap_zero_memory = 0x00000008; // HEAP_ZERO_MEMORY
257-
let zero_init = (flags & heap_zero_memory) == heap_zero_memory;
256+
const HEAP_ZERO_MEMORY: u32 = 0x00000008;
257+
let init = if (flags & HEAP_ZERO_MEMORY) == HEAP_ZERO_MEMORY {
258+
AllocInit::Zero
259+
} else {
260+
AllocInit::Uninit
261+
};
258262
// Alignment is twice the pointer size.
259263
// Source: <https://learn.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapalloc>
260264
let align = this.tcx.pointer_size().bytes().strict_mul(2);
261265
let ptr = this.allocate_ptr(
262266
Size::from_bytes(size),
263267
Align::from_bytes(align).unwrap(),
264268
MiriMemoryKind::WinHeap.into(),
265-
zero_init
269+
init
266270
)?;
267271
this.write_pointer(ptr, dest)?;
268272
}
@@ -295,7 +299,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
295299
Size::from_bytes(size),
296300
Align::from_bytes(align).unwrap(),
297301
MiriMemoryKind::WinHeap.into(),
298-
false
302+
AllocInit::Uninit
299303
)?;
300304
this.write_pointer(new_ptr, dest)?;
301305
}

0 commit comments

Comments
 (0)