Skip to content

Commit 3e5e541

Browse files
committed
Auto merge of #124084 - matthiaskrgr:rollup-h42psbx, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #116957 (meta: notify #t-rustdoc Zulip stream on backport nominations) - #122201 (Document overrides of `clone_from()` in core/std) - #122723 (Use same file permissions for ar_archive_writer as the LLVM archive writer) - #124030 (interpret: pass MemoryKind to adjust_alloc_base_pointer) - #124037 (Don't ascend into parent bodies when collecting stmts for possible return suggestion) - #124049 (Stabilize `const_io_structs`) - #124062 (Add another expression to weird-exprs.rs) - #124066 (Don't error on subtyping of equal types) - #124073 (Remove libc from rust_get_test_int uses) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a2128eb + 5688e83 commit 3e5e541

File tree

8 files changed

+46
-39
lines changed

8 files changed

+46
-39
lines changed

src/alloc_addresses/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
141141
}
142142
}
143143

144-
fn addr_from_alloc_id(&self, alloc_id: AllocId) -> InterpResult<'tcx, u64> {
144+
fn addr_from_alloc_id(
145+
&self,
146+
alloc_id: AllocId,
147+
_kind: MemoryKind,
148+
) -> InterpResult<'tcx, u64> {
145149
let ecx = self.eval_context_ref();
146150
let mut global_state = ecx.machine.alloc_addresses.borrow_mut();
147151
let global_state = &mut *global_state;
@@ -283,16 +287,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
283287
}
284288

285289
/// Convert a relative (tcx) pointer to a Miri pointer.
286-
fn ptr_from_rel_ptr(
290+
fn adjust_alloc_root_pointer(
287291
&self,
288292
ptr: Pointer<CtfeProvenance>,
289293
tag: BorTag,
294+
kind: MemoryKind,
290295
) -> InterpResult<'tcx, Pointer<Provenance>> {
291296
let ecx = self.eval_context_ref();
292297

293298
let (prov, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
294299
let alloc_id = prov.alloc_id();
295-
let base_addr = ecx.addr_from_alloc_id(alloc_id)?;
300+
let base_addr = ecx.addr_from_alloc_id(alloc_id, kind)?;
296301

297302
// Add offset with the right kind of pointer-overflowing arithmetic.
298303
let dl = ecx.data_layout();
@@ -314,9 +319,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
314319
ecx.alloc_id_from_addr(addr.bytes())?
315320
};
316321

317-
// This cannot fail: since we already have a pointer with that provenance, rel_ptr_to_addr
322+
// This cannot fail: since we already have a pointer with that provenance, adjust_alloc_root_pointer
318323
// must have been called in the past, so we can just look up the address in the map.
319-
let base_addr = ecx.addr_from_alloc_id(alloc_id).unwrap();
324+
let base_addr = *ecx.machine.alloc_addresses.borrow().base_addr.get(&alloc_id).unwrap();
320325

321326
// Wrapping "addr - base_addr"
322327
#[allow(clippy::cast_possible_wrap)] // we want to wrap here

src/borrow_tracker/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ pub struct GlobalStateInner {
8989
borrow_tracker_method: BorrowTrackerMethod,
9090
/// Next unused pointer ID (tag).
9191
next_ptr_tag: BorTag,
92-
/// Table storing the "base" tag for each allocation.
93-
/// The base tag is the one used for the initial pointer.
92+
/// Table storing the "root" tag for each allocation.
93+
/// The root tag is the one used for the initial pointer.
9494
/// We need this in a separate table to handle cyclic statics.
95-
base_ptr_tags: FxHashMap<AllocId, BorTag>,
95+
root_ptr_tags: FxHashMap<AllocId, BorTag>,
9696
/// Next unused call ID (for protectors).
9797
next_call_id: CallId,
9898
/// All currently protected tags.
@@ -175,7 +175,7 @@ impl GlobalStateInner {
175175
GlobalStateInner {
176176
borrow_tracker_method,
177177
next_ptr_tag: BorTag::one(),
178-
base_ptr_tags: FxHashMap::default(),
178+
root_ptr_tags: FxHashMap::default(),
179179
next_call_id: NonZero::new(1).unwrap(),
180180
protected_tags: FxHashMap::default(),
181181
tracked_pointer_tags,
@@ -213,8 +213,8 @@ impl GlobalStateInner {
213213
}
214214
}
215215

216-
pub fn base_ptr_tag(&mut self, id: AllocId, machine: &MiriMachine<'_, '_>) -> BorTag {
217-
self.base_ptr_tags.get(&id).copied().unwrap_or_else(|| {
216+
pub fn root_ptr_tag(&mut self, id: AllocId, machine: &MiriMachine<'_, '_>) -> BorTag {
217+
self.root_ptr_tags.get(&id).copied().unwrap_or_else(|| {
218218
let tag = self.new_ptr();
219219
if self.tracked_pointer_tags.contains(&tag) {
220220
machine.emit_diagnostic(NonHaltingDiagnostic::CreatedPointerTag(
@@ -223,14 +223,14 @@ impl GlobalStateInner {
223223
None,
224224
));
225225
}
226-
trace!("New allocation {:?} has base tag {:?}", id, tag);
227-
self.base_ptr_tags.try_insert(id, tag).unwrap();
226+
trace!("New allocation {:?} has rpot tag {:?}", id, tag);
227+
self.root_ptr_tags.try_insert(id, tag).unwrap();
228228
tag
229229
})
230230
}
231231

232232
pub fn remove_unreachable_allocs(&mut self, allocs: &LiveAllocs<'_, '_, '_>) {
233-
self.base_ptr_tags.retain(|id, _| allocs.is_live(*id));
233+
self.root_ptr_tags.retain(|id, _| allocs.is_live(*id));
234234
}
235235
}
236236

src/borrow_tracker/stacked_borrows/diagnostics.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn err_sb_ub<'tcx>(
2020
#[derive(Clone, Debug)]
2121
pub struct AllocHistory {
2222
id: AllocId,
23-
base: (Item, Span),
23+
root: (Item, Span),
2424
creations: smallvec::SmallVec<[Creation; 1]>,
2525
invalidations: smallvec::SmallVec<[Invalidation; 1]>,
2626
protectors: smallvec::SmallVec<[Protection; 1]>,
@@ -225,7 +225,7 @@ impl AllocHistory {
225225
pub fn new(id: AllocId, item: Item, machine: &MiriMachine<'_, '_>) -> Self {
226226
Self {
227227
id,
228-
base: (item, machine.current_span()),
228+
root: (item, machine.current_span()),
229229
creations: SmallVec::new(),
230230
invalidations: SmallVec::new(),
231231
protectors: SmallVec::new(),
@@ -342,15 +342,15 @@ impl<'history, 'ecx, 'mir, 'tcx> DiagnosticCx<'history, 'ecx, 'mir, 'tcx> {
342342
})
343343
})
344344
.or_else(|| {
345-
// If we didn't find a retag that created this tag, it might be the base tag of
345+
// If we didn't find a retag that created this tag, it might be the root tag of
346346
// this allocation.
347-
if self.history.base.0.tag() == tag {
347+
if self.history.root.0.tag() == tag {
348348
Some((
349349
format!(
350-
"{tag:?} was created here, as the base tag for {:?}",
350+
"{tag:?} was created here, as the root tag for {:?}",
351351
self.history.id
352352
),
353-
self.history.base.1.data(),
353+
self.history.root.1.data(),
354354
))
355355
} else {
356356
None

src/borrow_tracker/stacked_borrows/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ impl Stacks {
518518
// not through a pointer). That is, whenever we directly write to a local, this will pop
519519
// everything else off the stack, invalidating all previous pointers,
520520
// and in particular, *all* raw pointers.
521-
MemoryKind::Stack => (state.base_ptr_tag(id, machine), Permission::Unique),
521+
MemoryKind::Stack => (state.root_ptr_tag(id, machine), Permission::Unique),
522522
// Everything else is shared by default.
523-
_ => (state.base_ptr_tag(id, machine), Permission::SharedReadWrite),
523+
_ => (state.root_ptr_tag(id, machine), Permission::SharedReadWrite),
524524
};
525525
Stacks::new(size, perm, base_tag, id, machine)
526526
}

src/borrow_tracker/stacked_borrows/stack.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Stack {
4747
let mut first_removed = None;
4848

4949
// We never consider removing the bottom-most tag. For stacks without an unknown
50-
// bottom this preserves the base tag.
50+
// bottom this preserves the root tag.
5151
// Note that the algorithm below is based on considering the tag at read_idx - 1,
5252
// so precisely considering the tag at index 0 for removal when we have an unknown
5353
// bottom would complicate the implementation. The simplification of not considering
@@ -93,7 +93,7 @@ impl Stack {
9393
self.unique_range = 0..self.len();
9494
}
9595

96-
// Replace any Items which have been collected with the base item, a known-good value.
96+
// Replace any Items which have been collected with the root item, a known-good value.
9797
for i in 0..CACHE_LEN {
9898
if self.cache.idx[i] >= first_removed {
9999
self.cache.items[i] = self.borrows[0];
@@ -331,7 +331,7 @@ impl<'tcx> Stack {
331331
self.verify_cache_consistency();
332332
}
333333

334-
/// Construct a new `Stack` using the passed `Item` as the base tag.
334+
/// Construct a new `Stack` using the passed `Item` as the root tag.
335335
pub fn new(item: Item) -> Self {
336336
Stack {
337337
borrows: vec![item],
@@ -438,8 +438,8 @@ impl<'tcx> Stack {
438438
let mut removed = 0;
439439
let mut cursor = 0;
440440
// Remove invalid entries from the cache by rotating them to the end of the cache, then
441-
// keep track of how many invalid elements there are and overwrite them with the base tag.
442-
// The base tag here serves as a harmless default value.
441+
// keep track of how many invalid elements there are and overwrite them with the root tag.
442+
// The root tag here serves as a harmless default value.
443443
for _ in 0..CACHE_LEN - 1 {
444444
if self.cache.idx[cursor] >= start {
445445
self.cache.idx[cursor..CACHE_LEN - removed].rotate_left(1);

src/borrow_tracker/tree_borrows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'tcx> Tree {
3737
_kind: MemoryKind,
3838
machine: &MiriMachine<'_, 'tcx>,
3939
) -> Self {
40-
let tag = state.base_ptr_tag(id, machine); // Fresh tag for the root
40+
let tag = state.root_ptr_tag(id, machine); // Fresh tag for the root
4141
let span = machine.current_span();
4242
Tree::new(tag, size, span)
4343
}

src/machine.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ pub struct MiriMachine<'mir, 'tcx> {
503503
/// Crates which are considered local for the purposes of error reporting.
504504
pub(crate) local_crates: Vec<CrateNum>,
505505

506-
/// Mapping extern static names to their base pointer.
506+
/// Mapping extern static names to their pointer.
507507
extern_statics: FxHashMap<Symbol, Pointer<Provenance>>,
508508

509509
/// The random number generator used for resolving non-determinism.
@@ -1042,14 +1042,14 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
10421042
ecx.generate_nan(inputs)
10431043
}
10441044

1045-
fn thread_local_static_base_pointer(
1045+
fn thread_local_static_pointer(
10461046
ecx: &mut MiriInterpCx<'mir, 'tcx>,
10471047
def_id: DefId,
10481048
) -> InterpResult<'tcx, Pointer<Provenance>> {
10491049
ecx.get_or_create_thread_local_alloc(def_id)
10501050
}
10511051

1052-
fn extern_static_base_pointer(
1052+
fn extern_static_pointer(
10531053
ecx: &MiriInterpCx<'mir, 'tcx>,
10541054
def_id: DefId,
10551055
) -> InterpResult<'tcx, Pointer<Provenance>> {
@@ -1090,7 +1090,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
10901090
alloc: Cow<'b, Allocation>,
10911091
kind: Option<MemoryKind>,
10921092
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>> {
1093-
let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
1093+
let kind = kind.expect("we set our GLOBAL_KIND so this cannot be None");
10941094
if ecx.machine.tracked_alloc_ids.contains(&id) {
10951095
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(
10961096
id,
@@ -1135,7 +1135,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11351135
weak_memory: buffer_alloc,
11361136
backtrace,
11371137
},
1138-
|ptr| ecx.global_base_pointer(ptr),
1138+
|ptr| ecx.global_root_pointer(ptr),
11391139
)?;
11401140

11411141
if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) {
@@ -1148,31 +1148,33 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
11481148
Ok(Cow::Owned(alloc))
11491149
}
11501150

1151-
fn adjust_alloc_base_pointer(
1151+
fn adjust_alloc_root_pointer(
11521152
ecx: &MiriInterpCx<'mir, 'tcx>,
11531153
ptr: Pointer<CtfeProvenance>,
1154+
kind: Option<MemoryKind>,
11541155
) -> InterpResult<'tcx, Pointer<Provenance>> {
1156+
let kind = kind.expect("we set our GLOBAL_KIND so this cannot be None");
11551157
let alloc_id = ptr.provenance.alloc_id();
11561158
if cfg!(debug_assertions) {
11571159
// The machine promises to never call us on thread-local or extern statics.
11581160
match ecx.tcx.try_get_global_alloc(alloc_id) {
11591161
Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_thread_local_static(def_id) => {
1160-
panic!("adjust_alloc_base_pointer called on thread-local static")
1162+
panic!("adjust_alloc_root_pointer called on thread-local static")
11611163
}
11621164
Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_foreign_item(def_id) => {
1163-
panic!("adjust_alloc_base_pointer called on extern static")
1165+
panic!("adjust_alloc_root_pointer called on extern static")
11641166
}
11651167
_ => {}
11661168
}
11671169
}
11681170
// FIXME: can we somehow preserve the immutability of `ptr`?
11691171
let tag = if let Some(borrow_tracker) = &ecx.machine.borrow_tracker {
1170-
borrow_tracker.borrow_mut().base_ptr_tag(alloc_id, &ecx.machine)
1172+
borrow_tracker.borrow_mut().root_ptr_tag(alloc_id, &ecx.machine)
11711173
} else {
11721174
// Value does not matter, SB is disabled
11731175
BorTag::default()
11741176
};
1175-
ecx.ptr_from_rel_ptr(ptr, tag)
1177+
ecx.adjust_alloc_root_pointer(ptr, tag, kind)
11761178
}
11771179

11781180
/// Called on `usize as ptr` casts.

tests/fail/both_borrows/invalidate_against_protector3.stack.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | unsafe { *x = 0 };
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
9-
help: <TAG> was created here, as the base tag for ALLOC
9+
help: <TAG> was created here, as the root tag for ALLOC
1010
--> $DIR/invalidate_against_protector3.rs:LL:CC
1111
|
1212
LL | let ptr = alloc(Layout::for_value(&0i32)) as *mut i32;

0 commit comments

Comments
 (0)