Skip to content

Commit 9339bc6

Browse files
authored
Rollup merge of #138417 - RalfJung:interpret-cleanup, r=oli-obk
minor interpreter cleanups - remove the `eval_inline_asm` hook that `@saethlin` added; the usage never materialized and he agreed with removing it - I tried merging `init_alloc_extra` and `adjust_global_allocation` and it didn't work; leave a comment as to why. Also, make the allocation code path a bit more clear by renaming `init_alloc_extra` to `init_local_allocation`. r? `@oli-obk`
2 parents 6db8e5a + 03c1b43 commit 9339bc6

File tree

5 files changed

+85
-91
lines changed

5 files changed

+85
-91
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

+19-35
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::hash::Hash;
88

99
use rustc_abi::{Align, Size};
1010
use rustc_apfloat::{Float, FloatConvert};
11-
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1211
use rustc_middle::query::TyCtxtAt;
1312
use rustc_middle::ty::Ty;
1413
use rustc_middle::ty::layout::TyAndLayout;
@@ -21,7 +20,6 @@ use super::{
2120
AllocBytes, AllocId, AllocKind, AllocRange, Allocation, CTFE_ALLOC_SALT, ConstAllocation,
2221
CtfeProvenance, FnArg, Frame, ImmTy, InterpCx, InterpResult, MPlaceTy, MemoryKind,
2322
Misalignment, OpTy, PlaceTy, Pointer, Provenance, RangeSet, interp_ok, throw_unsup,
24-
throw_unsup_format,
2523
};
2624

2725
/// Data returned by [`Machine::after_stack_pop`], and consumed by
@@ -361,6 +359,19 @@ pub trait Machine<'tcx>: Sized {
361359
size: i64,
362360
) -> Option<(AllocId, Size, Self::ProvenanceExtra)>;
363361

362+
/// Return a "root" pointer for the given allocation: the one that is used for direct
363+
/// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
364+
///
365+
/// Not called on `extern` or thread-local statics (those use the methods above).
366+
///
367+
/// `kind` is the kind of the allocation the pointer points to; it can be `None` when
368+
/// it's a global and `GLOBAL_KIND` is `None`.
369+
fn adjust_alloc_root_pointer(
370+
ecx: &InterpCx<'tcx, Self>,
371+
ptr: Pointer,
372+
kind: Option<MemoryKind<Self::MemoryKind>>,
373+
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
374+
364375
/// Called to adjust global allocations to the Provenance and AllocExtra of this machine.
365376
///
366377
/// If `alloc` contains pointers, then they are all pointing to globals.
@@ -375,46 +386,19 @@ pub trait Machine<'tcx>: Sized {
375386
alloc: &'b Allocation,
376387
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>;
377388

378-
/// Initialize the extra state of an allocation.
389+
/// Initialize the extra state of an allocation local to this machine.
379390
///
380-
/// This is guaranteed to be called exactly once on all allocations that are accessed by the
381-
/// program.
382-
fn init_alloc_extra(
391+
/// This is guaranteed to be called exactly once on all allocations local to this machine.
392+
/// It will not be called automatically for global allocations; `adjust_global_allocation`
393+
/// has to do that itself if that is desired.
394+
fn init_local_allocation(
383395
ecx: &InterpCx<'tcx, Self>,
384396
id: AllocId,
385397
kind: MemoryKind<Self::MemoryKind>,
386398
size: Size,
387399
align: Align,
388400
) -> InterpResult<'tcx, Self::AllocExtra>;
389401

390-
/// Return a "root" pointer for the given allocation: the one that is used for direct
391-
/// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
392-
///
393-
/// Not called on `extern` or thread-local statics (those use the methods above).
394-
///
395-
/// `kind` is the kind of the allocation the pointer points to; it can be `None` when
396-
/// it's a global and `GLOBAL_KIND` is `None`.
397-
fn adjust_alloc_root_pointer(
398-
ecx: &InterpCx<'tcx, Self>,
399-
ptr: Pointer,
400-
kind: Option<MemoryKind<Self::MemoryKind>>,
401-
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
402-
403-
/// Evaluate the inline assembly.
404-
///
405-
/// This should take care of jumping to the next block (one of `targets`) when asm goto
406-
/// is triggered, `targets[0]` when the assembly falls through, or diverge in case of
407-
/// naked_asm! or `InlineAsmOptions::NORETURN` being set.
408-
fn eval_inline_asm(
409-
_ecx: &mut InterpCx<'tcx, Self>,
410-
_template: &'tcx [InlineAsmTemplatePiece],
411-
_operands: &[mir::InlineAsmOperand<'tcx>],
412-
_options: InlineAsmOptions,
413-
_targets: &[mir::BasicBlock],
414-
) -> InterpResult<'tcx> {
415-
throw_unsup_format!("inline assembly is not supported")
416-
}
417-
418402
/// Hook for performing extra checks on a memory read access.
419403
///
420404
/// This will *not* be called during validation!
@@ -699,7 +683,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
699683
interp_ok(Cow::Borrowed(alloc))
700684
}
701685

702-
fn init_alloc_extra(
686+
fn init_local_allocation(
703687
_ecx: &InterpCx<$tcx, Self>,
704688
_id: AllocId,
705689
_kind: MemoryKind<Self::MemoryKind>,

compiler/rustc_const_eval/src/interpret/memory.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
263263
M::GLOBAL_KIND.map(MemoryKind::Machine),
264264
"dynamically allocating global memory"
265265
);
266-
// We have set things up so we don't need to call `adjust_from_tcx` here,
267-
// so we avoid copying the entire allocation contents.
268-
let extra = M::init_alloc_extra(self, id, kind, alloc.size(), alloc.align)?;
266+
// This cannot be merged with the `adjust_global_allocation` code path
267+
// since here we have an allocation that already uses `M::Bytes`.
268+
let extra = M::init_local_allocation(self, id, kind, alloc.size(), alloc.align)?;
269269
let alloc = alloc.with_extra(extra);
270270
self.memory.alloc_map.insert(id, (kind, alloc));
271271
M::adjust_alloc_root_pointer(self, Pointer::from(id), Some(kind))

compiler/rustc_const_eval/src/interpret/step.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tracing::{info, instrument, trace};
1414

1515
use super::{
1616
FnArg, FnVal, ImmTy, Immediate, InterpCx, InterpResult, Machine, MemPlaceMeta, PlaceTy,
17-
Projectable, Scalar, interp_ok, throw_ub,
17+
Projectable, Scalar, interp_ok, throw_ub, throw_unsup_format,
1818
};
1919
use crate::util;
2020

@@ -590,8 +590,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
590590
terminator.kind
591591
),
592592

593-
InlineAsm { template, ref operands, options, ref targets, .. } => {
594-
M::eval_inline_asm(self, template, operands, options, targets)?;
593+
InlineAsm { .. } => {
594+
throw_unsup_format!("inline assembly is not supported");
595595
}
596596
}
597597

src/tools/miri/src/alloc_addresses/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
377377
fn get_global_alloc_bytes(
378378
&self,
379379
id: AllocId,
380-
kind: MemoryKind,
381380
bytes: &[u8],
382381
align: Align,
383382
) -> InterpResult<'tcx, MiriAllocBytes> {
@@ -386,7 +385,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
386385
// In native lib mode, MiriAllocBytes for global allocations are handled via `prepared_alloc_bytes`.
387386
// This additional call ensures that some `MiriAllocBytes` are always prepared, just in case
388387
// this function gets called before the first time `addr_from_alloc_id` gets called.
389-
this.addr_from_alloc_id(id, kind)?;
388+
this.addr_from_alloc_id(id, MiriMemoryKind::Global.into())?;
390389
// The memory we need here will have already been allocated during an earlier call to
391390
// `addr_from_alloc_id` for this allocation. So don't create a new `MiriAllocBytes` here, instead
392391
// fetch the previously prepared bytes from `prepared_alloc_bytes`.

src/tools/miri/src/machine.rs

+59-48
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,59 @@ impl<'tcx> MiriMachine<'tcx> {
814814
.and_then(|(_allocated, deallocated)| *deallocated)
815815
.map(Span::data)
816816
}
817+
818+
fn init_allocation(
819+
ecx: &MiriInterpCx<'tcx>,
820+
id: AllocId,
821+
kind: MemoryKind,
822+
size: Size,
823+
align: Align,
824+
) -> InterpResult<'tcx, AllocExtra<'tcx>> {
825+
if ecx.machine.tracked_alloc_ids.contains(&id) {
826+
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id, size, align, kind));
827+
}
828+
829+
let borrow_tracker = ecx
830+
.machine
831+
.borrow_tracker
832+
.as_ref()
833+
.map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine));
834+
835+
let data_race = ecx.machine.data_race.as_ref().map(|data_race| {
836+
data_race::AllocState::new_allocation(
837+
data_race,
838+
&ecx.machine.threads,
839+
size,
840+
kind,
841+
ecx.machine.current_span(),
842+
)
843+
});
844+
let weak_memory = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation);
845+
846+
// If an allocation is leaked, we want to report a backtrace to indicate where it was
847+
// allocated. We don't need to record a backtrace for allocations which are allowed to
848+
// leak.
849+
let backtrace = if kind.may_leak() || !ecx.machine.collect_leak_backtraces {
850+
None
851+
} else {
852+
Some(ecx.generate_stacktrace())
853+
};
854+
855+
if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) {
856+
ecx.machine
857+
.allocation_spans
858+
.borrow_mut()
859+
.insert(id, (ecx.machine.current_span(), None));
860+
}
861+
862+
interp_ok(AllocExtra {
863+
borrow_tracker,
864+
data_race,
865+
weak_memory,
866+
backtrace,
867+
sync: FxHashMap::default(),
868+
})
869+
}
817870
}
818871

819872
impl VisitProvenance for MiriMachine<'_> {
@@ -1200,57 +1253,15 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
12001253
}
12011254
}
12021255

1203-
fn init_alloc_extra(
1256+
fn init_local_allocation(
12041257
ecx: &MiriInterpCx<'tcx>,
12051258
id: AllocId,
12061259
kind: MemoryKind,
12071260
size: Size,
12081261
align: Align,
12091262
) -> InterpResult<'tcx, Self::AllocExtra> {
1210-
if ecx.machine.tracked_alloc_ids.contains(&id) {
1211-
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id, size, align, kind));
1212-
}
1213-
1214-
let borrow_tracker = ecx
1215-
.machine
1216-
.borrow_tracker
1217-
.as_ref()
1218-
.map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine));
1219-
1220-
let data_race = ecx.machine.data_race.as_ref().map(|data_race| {
1221-
data_race::AllocState::new_allocation(
1222-
data_race,
1223-
&ecx.machine.threads,
1224-
size,
1225-
kind,
1226-
ecx.machine.current_span(),
1227-
)
1228-
});
1229-
let weak_memory = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation);
1230-
1231-
// If an allocation is leaked, we want to report a backtrace to indicate where it was
1232-
// allocated. We don't need to record a backtrace for allocations which are allowed to
1233-
// leak.
1234-
let backtrace = if kind.may_leak() || !ecx.machine.collect_leak_backtraces {
1235-
None
1236-
} else {
1237-
Some(ecx.generate_stacktrace())
1238-
};
1239-
1240-
if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) {
1241-
ecx.machine
1242-
.allocation_spans
1243-
.borrow_mut()
1244-
.insert(id, (ecx.machine.current_span(), None));
1245-
}
1246-
1247-
interp_ok(AllocExtra {
1248-
borrow_tracker,
1249-
data_race,
1250-
weak_memory,
1251-
backtrace,
1252-
sync: FxHashMap::default(),
1253-
})
1263+
assert!(kind != MiriMemoryKind::Global.into());
1264+
MiriMachine::init_allocation(ecx, id, kind, size, align)
12541265
}
12551266

12561267
fn adjust_alloc_root_pointer(
@@ -1340,13 +1351,13 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
13401351
alloc: &'b Allocation,
13411352
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>
13421353
{
1343-
let kind = Self::GLOBAL_KIND.unwrap().into();
13441354
let alloc = alloc.adjust_from_tcx(
13451355
&ecx.tcx,
1346-
|bytes, align| ecx.get_global_alloc_bytes(id, kind, bytes, align),
1356+
|bytes, align| ecx.get_global_alloc_bytes(id, bytes, align),
13471357
|ptr| ecx.global_root_pointer(ptr),
13481358
)?;
1349-
let extra = Self::init_alloc_extra(ecx, id, kind, alloc.size(), alloc.align)?;
1359+
let kind = MiriMemoryKind::Global.into();
1360+
let extra = MiriMachine::init_allocation(ecx, id, kind, alloc.size(), alloc.align)?;
13501361
interp_ok(Cow::Owned(alloc.with_extra(extra)))
13511362
}
13521363

0 commit comments

Comments
 (0)