Skip to content

Commit c422581

Browse files
committed
Auto merge of rust-lang#127317 - RalfJung:miri-sync, r=RalfJung
Miri subtree update r? `@ghost`
2 parents 8a9cccb + a3460e2 commit c422581

File tree

15 files changed

+192
-71
lines changed

15 files changed

+192
-71
lines changed

Diff for: src/tools/miri/rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9ed2ab3790ff41bf741dd690befd6a1c1e2b23ca
1+
66b4f0021bfb11a8c20d084c99a40f4a78ce1d38

Diff for: src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum AccessCause {
1919
Explicit(AccessKind),
2020
Reborrow,
2121
Dealloc,
22-
FnExit,
22+
FnExit(AccessKind),
2323
}
2424

2525
impl fmt::Display for AccessCause {
@@ -28,7 +28,11 @@ impl fmt::Display for AccessCause {
2828
Self::Explicit(kind) => write!(f, "{kind}"),
2929
Self::Reborrow => write!(f, "reborrow"),
3030
Self::Dealloc => write!(f, "deallocation"),
31-
Self::FnExit => write!(f, "protector release"),
31+
// This is dead code, since the protector release access itself can never
32+
// cause UB (while the protector is active, if some other access invalidates
33+
// further use of the protected tag, that is immediate UB).
34+
// Describing the cause of UB is the only time this function is called.
35+
Self::FnExit(_) => unreachable!("protector accesses can never be the source of UB"),
3236
}
3337
}
3438
}
@@ -40,7 +44,7 @@ impl AccessCause {
4044
Self::Explicit(kind) => format!("{rel} {kind}"),
4145
Self::Reborrow => format!("reborrow (acting as a {rel} read access)"),
4246
Self::Dealloc => format!("deallocation (acting as a {rel} write access)"),
43-
Self::FnExit => format!("protector release (acting as a {rel} read access)"),
47+
Self::FnExit(kind) => format!("protector release (acting as a {rel} {kind})"),
4448
}
4549
}
4650
}

Diff for: src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ impl<'tcx> Tree {
6868
let global = machine.borrow_tracker.as_ref().unwrap();
6969
let span = machine.current_span();
7070
self.perform_access(
71-
access_kind,
7271
tag,
73-
Some(range),
72+
Some((range, access_kind, diagnostics::AccessCause::Explicit(access_kind))),
7473
global,
7574
alloc_id,
7675
span,
77-
diagnostics::AccessCause::Explicit(access_kind),
7876
)
7977
}
8078

@@ -115,15 +113,8 @@ impl<'tcx> Tree {
115113
alloc_id: AllocId, // diagnostics
116114
) -> InterpResult<'tcx> {
117115
let span = machine.current_span();
118-
self.perform_access(
119-
AccessKind::Read,
120-
tag,
121-
None, // no specified range because it occurs on the entire allocation
122-
global,
123-
alloc_id,
124-
span,
125-
diagnostics::AccessCause::FnExit,
126-
)
116+
// `None` makes it the magic on-protector-end operation
117+
self.perform_access(tag, None, global, alloc_id, span)
127118
}
128119
}
129120

@@ -297,13 +288,11 @@ trait EvalContextPrivExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
297288

298289
// All reborrows incur a (possibly zero-sized) read access to the parent
299290
tree_borrows.perform_access(
300-
AccessKind::Read,
301291
orig_tag,
302-
Some(range),
292+
Some((range, AccessKind::Read, diagnostics::AccessCause::Reborrow)),
303293
this.machine.borrow_tracker.as_ref().unwrap(),
304294
alloc_id,
305295
this.machine.current_span(),
306-
diagnostics::AccessCause::Reborrow,
307296
)?;
308297
// Record the parent-child pair in the tree.
309298
tree_borrows.new_child(orig_tag, new_tag, new_perm.initial_state, range, span)?;

Diff for: src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs

+4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ impl Permission {
186186
pub fn is_disabled(&self) -> bool {
187187
self.inner == Disabled
188188
}
189+
/// Check if `self` is the post-child-write state of a pointer (is `Active`).
190+
pub fn is_active(&self) -> bool {
191+
self.inner == Active
192+
}
189193

190194
/// Default initial permission of the root of a new tree at inbounds positions.
191195
/// Must *only* be used for the root, this is not in general an "initial" permission!

Diff for: src/tools/miri/src/borrow_tracker/tree_borrows/tree.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,11 @@ impl<'tcx> Tree {
530530
span: Span, // diagnostics
531531
) -> InterpResult<'tcx> {
532532
self.perform_access(
533-
AccessKind::Write,
534533
tag,
535-
Some(access_range),
534+
Some((access_range, AccessKind::Write, diagnostics::AccessCause::Dealloc)),
536535
global,
537536
alloc_id,
538537
span,
539-
diagnostics::AccessCause::Dealloc,
540538
)?;
541539
for (perms_range, perms) in self.rperms.iter_mut(access_range.start, access_range.size) {
542540
TreeVisitor { nodes: &mut self.nodes, tag_mapping: &self.tag_mapping, perms }
@@ -570,12 +568,16 @@ impl<'tcx> Tree {
570568
}
571569

572570
/// Map the per-node and per-location `LocationState::perform_access`
573-
/// to each location of `access_range`, on every tag of the allocation.
571+
/// to each location of the first component of `access_range_and_kind`,
572+
/// on every tag of the allocation.
574573
///
575-
/// If `access_range` is `None`, this is interpreted as the special
574+
/// If `access_range_and_kind` is `None`, this is interpreted as the special
576575
/// access that is applied on protector release:
577576
/// - the access will be applied only to initialized locations of the allocation,
578-
/// - and it will not be visible to children.
577+
/// - it will not be visible to children,
578+
/// - it will be recorded as a `FnExit` diagnostic access
579+
/// - and it will be a read except if the location is `Active`, i.e. has been written to,
580+
/// in which case it will be a write.
579581
///
580582
/// `LocationState::perform_access` will take care of raising transition
581583
/// errors and updating the `initialized` status of each location,
@@ -585,13 +587,11 @@ impl<'tcx> Tree {
585587
/// - recording the history.
586588
pub fn perform_access(
587589
&mut self,
588-
access_kind: AccessKind,
589590
tag: BorTag,
590-
access_range: Option<AllocRange>,
591+
access_range_and_kind: Option<(AllocRange, AccessKind, diagnostics::AccessCause)>,
591592
global: &GlobalState,
592-
alloc_id: AllocId, // diagnostics
593-
span: Span, // diagnostics
594-
access_cause: diagnostics::AccessCause, // diagnostics
593+
alloc_id: AllocId, // diagnostics
594+
span: Span, // diagnostics
595595
) -> InterpResult<'tcx> {
596596
use std::ops::Range;
597597
// Performs the per-node work:
@@ -605,6 +605,8 @@ impl<'tcx> Tree {
605605
// `perms_range` is only for diagnostics (it is the range of
606606
// the `RangeMap` on which we are currently working).
607607
let node_app = |perms_range: Range<u64>,
608+
access_kind: AccessKind,
609+
access_cause: diagnostics::AccessCause,
608610
args: NodeAppArgs<'_>|
609611
-> Result<ContinueTraversal, TransitionError> {
610612
let NodeAppArgs { node, mut perm, rel_pos } = args;
@@ -618,14 +620,13 @@ impl<'tcx> Tree {
618620

619621
let protected = global.borrow().protected_tags.contains_key(&node.tag);
620622
let transition = old_state.perform_access(access_kind, rel_pos, protected)?;
621-
622623
// Record the event as part of the history
623624
if !transition.is_noop() {
624625
node.debug_info.history.push(diagnostics::Event {
625626
transition,
626627
is_foreign: rel_pos.is_foreign(),
627628
access_cause,
628-
access_range,
629+
access_range: access_range_and_kind.map(|x| x.0),
629630
transition_range: perms_range,
630631
span,
631632
});
@@ -636,6 +637,7 @@ impl<'tcx> Tree {
636637
// Error handler in case `node_app` goes wrong.
637638
// Wraps the faulty transition in more context for diagnostics.
638639
let err_handler = |perms_range: Range<u64>,
640+
access_cause: diagnostics::AccessCause,
639641
args: ErrHandlerArgs<'_, TransitionError>|
640642
-> InterpError<'tcx> {
641643
let ErrHandlerArgs { error_kind, conflicting_info, accessed_info } = args;
@@ -650,16 +652,16 @@ impl<'tcx> Tree {
650652
.build()
651653
};
652654

653-
if let Some(access_range) = access_range {
655+
if let Some((access_range, access_kind, access_cause)) = access_range_and_kind {
654656
// Default branch: this is a "normal" access through a known range.
655657
// We iterate over affected locations and traverse the tree for each of them.
656658
for (perms_range, perms) in self.rperms.iter_mut(access_range.start, access_range.size)
657659
{
658660
TreeVisitor { nodes: &mut self.nodes, tag_mapping: &self.tag_mapping, perms }
659661
.traverse_parents_this_children_others(
660662
tag,
661-
|args| node_app(perms_range.clone(), args),
662-
|args| err_handler(perms_range.clone(), args),
663+
|args| node_app(perms_range.clone(), access_kind, access_cause, args),
664+
|args| err_handler(perms_range.clone(), access_cause, args),
663665
)?;
664666
}
665667
} else {
@@ -678,11 +680,14 @@ impl<'tcx> Tree {
678680
if let Some(p) = perms.get(idx)
679681
&& p.initialized
680682
{
683+
let access_kind =
684+
if p.permission.is_active() { AccessKind::Write } else { AccessKind::Read };
685+
let access_cause = diagnostics::AccessCause::FnExit(access_kind);
681686
TreeVisitor { nodes: &mut self.nodes, tag_mapping: &self.tag_mapping, perms }
682687
.traverse_nonchildren(
683688
tag,
684-
|args| node_app(perms_range.clone(), args),
685-
|args| err_handler(perms_range.clone(), args),
689+
|args| node_app(perms_range.clone(), access_kind, access_cause, args),
690+
|args| err_handler(perms_range.clone(), access_cause, args),
686691
)?;
687692
}
688693
}

Diff for: src/tools/miri/src/helpers.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::ty::{
2424
FloatTy, IntTy, Ty, TyCtxt, UintTy,
2525
};
2626
use rustc_session::config::CrateType;
27-
use rustc_span::{sym, Span, Symbol};
27+
use rustc_span::{Span, Symbol};
2828
use rustc_target::abi::{Align, FieldIdx, FieldsShape, Size, Variants};
2929
use rustc_target::spec::abi::Abi;
3030

@@ -1182,14 +1182,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11821182
this.alloc_mark_immutable(provenance.get_alloc_id().unwrap()).unwrap();
11831183
}
11841184

1185-
fn item_link_name(&self, def_id: DefId) -> Symbol {
1186-
let tcx = self.eval_context_ref().tcx;
1187-
match tcx.get_attrs(def_id, sym::link_name).filter_map(|a| a.value_str()).next() {
1188-
Some(name) => name,
1189-
None => tcx.item_name(def_id),
1190-
}
1191-
}
1192-
11931185
/// Converts `src` from floating point to integer type `dest_ty`
11941186
/// after rounding with mode `round`.
11951187
/// Returns `None` if `f` is NaN or out of range.

Diff for: src/tools/miri/src/machine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
954954
// foreign function
955955
// Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
956956
let args = ecx.copy_fn_args(args); // FIXME: Should `InPlace` arguments be reset to uninit?
957-
let link_name = ecx.item_link_name(instance.def_id());
957+
let link_name = Symbol::intern(ecx.tcx.symbol_name(instance).name);
958958
return ecx.emulate_foreign_item(link_name, abi, &args, dest, ret, unwind);
959959
}
960960

@@ -1050,7 +1050,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
10501050
ecx: &MiriInterpCx<'tcx>,
10511051
def_id: DefId,
10521052
) -> InterpResult<'tcx, StrictPointer> {
1053-
let link_name = ecx.item_link_name(def_id);
1053+
let link_name = Symbol::intern(ecx.tcx.symbol_name(Instance::mono(*ecx.tcx, def_id)).name);
10541054
if let Some(&ptr) = ecx.machine.extern_statics.get(&link_name) {
10551055
// Various parts of the engine rely on `get_alloc_info` for size and alignment
10561056
// information. That uses the type information of this static.

Diff for: src/tools/miri/src/shims/foreign_items.rs

-15
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4646
unwind: mir::UnwindAction,
4747
) -> InterpResult<'tcx, Option<(&'tcx mir::Body<'tcx>, ty::Instance<'tcx>)>> {
4848
let this = self.eval_context_mut();
49-
let tcx = this.tcx.tcx;
5049

5150
// Some shims forward to other MIR bodies.
5251
match link_name.as_str() {
53-
// This matches calls to the foreign item `panic_impl`.
54-
// The implementation is provided by the function with the `#[panic_handler]` attribute.
55-
"panic_impl" => {
56-
// We don't use `check_shim` here because we are just forwarding to the lang
57-
// item. Argument count checking will be performed when the returned `Body` is
58-
// called.
59-
this.check_abi_and_shim_symbol_clash(abi, Abi::Rust, link_name)?;
60-
let panic_impl_id = tcx.lang_items().panic_impl().unwrap();
61-
let panic_impl_instance = ty::Instance::mono(tcx, panic_impl_id);
62-
return Ok(Some((
63-
this.load_mir(panic_impl_instance.def, None)?,
64-
panic_impl_instance,
65-
)));
66-
}
6752
"__rust_alloc_error_handler" => {
6853
// Forward to the right symbol that implements this function.
6954
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {

Diff for: src/tools/miri/src/shims/unix/fd.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,32 @@ impl FdTable {
273273

274274
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
275275
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
276+
fn dup(&mut self, old_fd: i32) -> InterpResult<'tcx, i32> {
277+
let this = self.eval_context_mut();
278+
279+
let Some(dup_fd) = this.machine.fds.dup(old_fd) else {
280+
return this.fd_not_found();
281+
};
282+
Ok(this.machine.fds.insert_fd_with_min_fd(dup_fd, 0))
283+
}
284+
285+
fn dup2(&mut self, old_fd: i32, new_fd: i32) -> InterpResult<'tcx, i32> {
286+
let this = self.eval_context_mut();
287+
288+
let Some(dup_fd) = this.machine.fds.dup(old_fd) else {
289+
return this.fd_not_found();
290+
};
291+
if new_fd != old_fd {
292+
// Close new_fd if it is previously opened.
293+
// If old_fd and new_fd point to the same description, then `dup_fd` ensures we keep the underlying file description alive.
294+
if let Some(file_descriptor) = this.machine.fds.fds.insert(new_fd, dup_fd) {
295+
// Ignore close error (not interpreter's) according to dup2() doc.
296+
file_descriptor.close(this.machine.communicate())?.ok();
297+
}
298+
}
299+
Ok(new_fd)
300+
}
301+
276302
fn fcntl(&mut self, args: &[OpTy<'tcx>]) -> InterpResult<'tcx, i32> {
277303
let this = self.eval_context_mut();
278304

@@ -334,14 +360,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
334360

335361
let fd = this.read_scalar(fd_op)?.to_i32()?;
336362

337-
Ok(Scalar::from_i32(if let Some(file_descriptor) = this.machine.fds.remove(fd) {
338-
let result = file_descriptor.close(this.machine.communicate())?;
339-
// return `0` if close is successful
340-
let result = result.map(|()| 0i32);
341-
this.try_unwrap_io_result(result)?
342-
} else {
343-
this.fd_not_found()?
344-
}))
363+
let Some(file_descriptor) = this.machine.fds.remove(fd) else {
364+
return Ok(Scalar::from_i32(this.fd_not_found()?));
365+
};
366+
let result = file_descriptor.close(this.machine.communicate())?;
367+
// return `0` if close is successful
368+
let result = result.map(|()| 0i32);
369+
Ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
345370
}
346371

347372
/// Function used when a file descriptor does not exist. It returns `Ok(-1)`and sets

Diff for: src/tools/miri/src/shims/unix/foreign_items.rs

+13
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
115115
let result = this.fcntl(args)?;
116116
this.write_scalar(Scalar::from_i32(result), dest)?;
117117
}
118+
"dup" => {
119+
let [old_fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
120+
let old_fd = this.read_scalar(old_fd)?.to_i32()?;
121+
let new_fd = this.dup(old_fd)?;
122+
this.write_scalar(Scalar::from_i32(new_fd), dest)?;
123+
}
124+
"dup2" => {
125+
let [old_fd, new_fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
126+
let old_fd = this.read_scalar(old_fd)?.to_i32()?;
127+
let new_fd = this.read_scalar(new_fd)?.to_i32()?;
128+
let result = this.dup2(old_fd, new_fd)?;
129+
this.write_scalar(Scalar::from_i32(result), dest)?;
130+
}
118131

119132
// File and file system access
120133
"open" | "open64" => {

Diff for: src/tools/miri/src/shims/x86/avx.rs

+11
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
338338

339339
this.write_scalar(Scalar::from_i32(res.into()), dest)?;
340340
}
341+
// Used to implement the `_mm256_zeroupper` and `_mm256_zeroall` functions.
342+
// These function clear out the upper 128 bits of all avx registers or
343+
// zero out all avx registers respectively.
344+
"vzeroupper" | "vzeroall" => {
345+
// These functions are purely a performance hint for the CPU.
346+
// Any registers currently in use will be saved beforehand by the
347+
// compiler, making these functions no-ops.
348+
349+
// The only thing that needs to be ensured is the correct calling convention.
350+
let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
351+
}
341352
_ => return Ok(EmulateItemResult::NotSupported),
342353
}
343354
Ok(EmulateItemResult::NeedsReturn)

0 commit comments

Comments
 (0)