Skip to content

Commit

Permalink
also print 'immutable' flag
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 26, 2023
1 parent df97862 commit 37a50ac
Show file tree
Hide file tree
Showing 40 changed files with 139 additions and 138 deletions.
37 changes: 12 additions & 25 deletions compiler/rustc_middle/src/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,7 @@ pub trait Provenance: Copy + fmt::Debug + 'static {
const OFFSET_IS_ADDR: bool;

/// Determines how a pointer should be printed.
///
/// Default impl is only good for when `OFFSET_IS_ADDR == true`.
fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result
where
Self: Sized,
{
assert!(Self::OFFSET_IS_ADDR);
let (prov, addr) = ptr.into_parts(); // address is absolute
write!(f, "{:#x}", addr.bytes())?;
if f.alternate() {
write!(f, "{prov:#?}")?;
} else {
write!(f, "{prov:?}")?;
}
Ok(())
}
fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;

/// If `OFFSET_IS_ADDR == false`, provenance must always be able to
/// identify the allocation this ptr points to (i.e., this must return `Some`).
Expand All @@ -156,8 +141,11 @@ impl From<AllocId> for CtfeProvenance {

impl fmt::Debug for CtfeProvenance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// FIXME print "immutable" bit
self.alloc_id().fmt(f)
fmt::Debug::fmt(&self.alloc_id(), f)?; // propagates `alternate` flag
if self.immutable() {
write!(f, "<imm>")?;
}
Ok(())
}
}

Expand Down Expand Up @@ -189,17 +177,16 @@ impl Provenance for CtfeProvenance {
const OFFSET_IS_ADDR: bool = false;

fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// FIXME print "immutable" bit
// Forward `alternate` flag to `alloc_id` printing.
if f.alternate() {
write!(f, "{:#?}", ptr.provenance.alloc_id())?;
} else {
write!(f, "{:?}", ptr.provenance.alloc_id())?;
}
// Print AllocId.
fmt::Debug::fmt(&ptr.provenance.alloc_id(), f)?; // propagates `alternate` flag
// Print offset only if it is non-zero.
if ptr.offset.bytes() > 0 {
write!(f, "+{:#x}", ptr.offset.bytes())?;
}
// Print immutable status.
if ptr.provenance.immutable() {
write!(f, "<imm>")?;
}
Ok(())
}

Expand Down
7 changes: 5 additions & 2 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4248,15 +4248,18 @@ impl<'test> TestCx<'test> {
let mut seen_allocs = indexmap::IndexSet::new();

// The alloc-id appears in pretty-printed allocations.
let re = Regex::new(r"╾─*a(lloc)?([0-9]+)(\+0x[0-9]+)?─*╼").unwrap();
let re =
Regex::new(r"╾─*a(lloc)?([0-9]+)(\+0x[0-9]+)?(<imm>)?( \([0-9]+ ptr bytes\))?─*╼")
.unwrap();
normalized = re
.replace_all(&normalized, |caps: &Captures<'_>| {
// Renumber the captured index.
let index = caps.get(2).unwrap().as_str().to_string();
let (index, _) = seen_allocs.insert_full(index);
let offset = caps.get(3).map_or("", |c| c.as_str());
let imm = caps.get(4).map_or("", |c| c.as_str());
// Do not bother keeping it pretty, just make it deterministic.
format!("╾ALLOC{index}{offset}╼")
format!("╾ALLOC{index}{offset}{imm}╼")
})
.into_owned();

Expand Down
11 changes: 11 additions & 0 deletions src/tools/miri/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ impl interpret::Provenance for Provenance {
}
}

fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (prov, addr) = ptr.into_parts(); // address is absolute
write!(f, "{:#x}", addr.bytes())?;
if f.alternate() {
write!(f, "{prov:#?}")?;
} else {
write!(f, "{prov:?}")?;
}
Ok(())
}

fn join(left: Option<Self>, right: Option<Self>) -> Option<Self> {
match (left, right) {
// If both are the *same* concrete tag, that is the result.
Expand Down
14 changes: 7 additions & 7 deletions tests/mir-opt/const_allocation.main.ConstProp.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ fn main() -> () {
}

ALLOC9 (static: FOO, size: 8, align: 4) {
ALLOC003 00 00 00 │ ╾──╼....
ALLOC0<imm>03 00 00 00 │ ╾──╼....
}

ALLOC0 (size: 48, align: 4) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC100 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾ALLOC202 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00ALLOC303 00 00 00 │ ....*...╾──╼....
0x0000 00 00 00 __ __ __ __ ╾ALLOC1<imm>00 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾ALLOC2<imm>02 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00ALLOC3<imm>03 00 00 00 │ ....*...╾──╼....
}

ALLOC1 (size: 0, align: 4) {}

ALLOC2 (size: 16, align: 4) {
ALLOC403 00 00 00ALLOC503 00 00 00 │ ╾──╼....╾──╼....
ALLOC4<imm>03 00 00 00ALLOC5<imm>03 00 00 00 │ ╾──╼....╾──╼....
}

ALLOC4 (size: 3, align: 1) {
Expand All @@ -42,8 +42,8 @@ ALLOC5 (size: 3, align: 1) {
}

ALLOC3 (size: 24, align: 4) {
0x00 │ ╾ALLOC603 00 00 00ALLOC703 00 00 00 │ ╾──╼....╾──╼....
0x10 │ ╾ALLOC804 00 00 00 │ ╾──╼....
0x00 │ ╾ALLOC6<imm>03 00 00 00ALLOC7<imm>03 00 00 00 │ ╾──╼....╾──╼....
0x10 │ ╾ALLOC8<imm>04 00 00 00 │ ╾──╼....
}

ALLOC6 (size: 3, align: 1) {
Expand Down
18 changes: 9 additions & 9 deletions tests/mir-opt/const_allocation.main.ConstProp.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ fn main() -> () {
}

ALLOC9 (static: FOO, size: 16, align: 8) {
ALLOC003 00 00 00 00 00 00 00 │ ╾──────╼........
ALLOC0<imm>03 00 00 00 00 00 00 00 │ ╾──────╼........
}

ALLOC0 (size: 72, align: 8) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC1╼ │ ....░░░░╾──────╼
0x0000 00 00 00 __ __ __ __ ╾ALLOC1<imm>╼ │ ....░░░░╾──────╼
0x1000 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
0x20 │ ╾ALLOC202 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00ALLOC3╼ │ ....*...╾──────╼
0x20 │ ╾ALLOC2<imm>02 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00ALLOC3<imm>╼ │ ....*...╾──────╼
0x4003 00 00 00 00 00 00 00 │ ........
}

ALLOC1 (size: 0, align: 8) {}

ALLOC2 (size: 32, align: 8) {
0x00 │ ╾ALLOC403 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾ALLOC503 00 00 00 00 00 00 00 │ ╾──────╼........
0x00 │ ╾ALLOC4<imm>03 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾ALLOC5<imm>03 00 00 00 00 00 00 00 │ ╾──────╼........
}

ALLOC4 (size: 3, align: 1) {
Expand All @@ -45,9 +45,9 @@ ALLOC5 (size: 3, align: 1) {
}

ALLOC3 (size: 48, align: 8) {
0x00 │ ╾ALLOC603 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾ALLOC703 00 00 00 00 00 00 00 │ ╾──────╼........
0x20 │ ╾ALLOC804 00 00 00 00 00 00 00 │ ╾──────╼........
0x00 │ ╾ALLOC6<imm>03 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾ALLOC7<imm>03 00 00 00 00 00 00 00 │ ╾──────╼........
0x20 │ ╾ALLOC8<imm>04 00 00 00 00 00 00 00 │ ╾──────╼........
}

ALLOC6 (size: 3, align: 1) {
Expand Down
12 changes: 6 additions & 6 deletions tests/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ fn main() -> () {
}

ALLOC9 (static: FOO, size: 8, align: 4) {
ALLOC003 00 00 00 │ ╾──╼....
ALLOC0<imm>03 00 00 00 │ ╾──╼....
}

ALLOC0 (size: 48, align: 4) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC100 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾ALLOC202 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00ALLOC303 00 00 00 │ ....*...╾──╼....
0x0000 00 00 00 __ __ __ __ ╾ALLOC1<imm>00 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾ALLOC2<imm>02 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00ALLOC3<imm>03 00 00 00 │ ....*...╾──╼....
}

ALLOC1 (size: 0, align: 4) {}

ALLOC2 (size: 8, align: 4) {
ALLOC4╼ ╾ALLOC5╼ │ ╾──╼╾──╼
ALLOC4<imm>╼ ╾ALLOC5<imm>╼ │ ╾──╼╾──╼
}

ALLOC4 (size: 1, align: 1) {
Expand All @@ -42,7 +42,7 @@ ALLOC5 (size: 1, align: 1) {
}

ALLOC3 (size: 12, align: 4) {
ALLOC6+0x3╼ ╾ALLOC7╼ ╾ALLOC8+0x2╼ │ ╾──╼╾──╼╾──╼
ALLOC6+0x3<imm>╼ ╾ALLOC7<imm>╼ ╾ALLOC8+0x2<imm>╼ │ ╾──╼╾──╼╾──╼
}

ALLOC6 (size: 4, align: 1) {
Expand Down
14 changes: 7 additions & 7 deletions tests/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ fn main() -> () {
}

ALLOC9 (static: FOO, size: 16, align: 8) {
ALLOC003 00 00 00 00 00 00 00 │ ╾──────╼........
ALLOC0<imm>03 00 00 00 00 00 00 00 │ ╾──────╼........
}

ALLOC0 (size: 72, align: 8) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC1╼ │ ....░░░░╾──────╼
0x0000 00 00 00 __ __ __ __ ╾ALLOC1<imm>╼ │ ....░░░░╾──────╼
0x1000 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
0x20 │ ╾ALLOC202 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00ALLOC3╼ │ ....*...╾──────╼
0x20 │ ╾ALLOC2<imm>02 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00ALLOC3<imm>╼ │ ....*...╾──────╼
0x4003 00 00 00 00 00 00 00 │ ........
}

ALLOC1 (size: 0, align: 8) {}

ALLOC2 (size: 16, align: 8) {
ALLOC4╼ ╾ALLOC5╼ │ ╾──────╼╾──────╼
ALLOC4<imm>╼ ╾ALLOC5<imm>╼ │ ╾──────╼╾──────╼
}

ALLOC4 (size: 1, align: 1) {
Expand All @@ -44,8 +44,8 @@ ALLOC5 (size: 1, align: 1) {
}

ALLOC3 (size: 24, align: 8) {
0x00 │ ╾ALLOC6+0x3╼ ╾ALLOC7╼ │ ╾──────╼╾──────╼
0x10 │ ╾ALLOC8+0x2╼ │ ╾──────╼
0x00 │ ╾ALLOC6+0x3<imm>╼ ╾ALLOC7<imm>╼ │ ╾──────╼╾──────╼
0x10 │ ╾ALLOC8+0x2<imm>╼ │ ╾──────╼
}

ALLOC6 (size: 4, align: 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ fn main() -> () {
}

ALLOC4 (static: FOO, size: 4, align: 4) {
ALLOC0╼ │ ╾──╼
ALLOC0<imm>╼ │ ╾──╼
}

ALLOC0 (size: 168, align: 1) {
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾ALLOC1╼ │ ............╾──╼
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾ALLOC1<imm>╼ │ ............╾──╼
0x2001 ef cd ab 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x3000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x4000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x5000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x6000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x7000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x8000 00 00 00 00 00 00 00 00 00ALLOC200 00 │ ..........╾──╼..
0x90 │ ╾ALLOC3+0x6300 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
0x90 │ ╾ALLOC3+0x63<imm>00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
0xa000 00 00 00 00 00 00 00 │ ........
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ fn main() -> () {
}

ALLOC2 (static: FOO, size: 8, align: 8) {
ALLOC0╼ │ ╾──────╼
ALLOC0<imm>╼ │ ╾──────╼
}

ALLOC0 (size: 180, align: 1) {
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾──ALLOC3── │ ............╾───
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾ALLOC3<imm> (8 ptr bytes) │ ............╾───
0x20 │ ──────────╼ 01 ef cd ab 00 00 00 00 00 00 00 00 │ ───╼............
0x3000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x4000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x5000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x6000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x7000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x8000 00 00 00 00 00 00 00 00 00 00 00 00 00 ╾──── │ ..............╾─
0x90 │ ─────ALLOC4─────╼ 00 00ALLOC1+0x63╼ │ ─────╼..╾──────╼
0x90 │ ─────ALLOC4─────╼ 00 00ALLOC1+0x63<imm>╼ │ ─────╼..╾──────╼
0xa000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0xb000 00 00 00 │ ....
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
}

ALLOC2 (static: RC, size: 4, align: 4) {
╾ALLOC0╼ │ ╾──╼
╾ALLOC0<imm>╼ │ ╾──╼
}

ALLOC0 (size: 8, align: 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
}

ALLOC2 (static: RC, size: 8, align: 8) {
╾ALLOC0╼ │ ╾──────╼
╾ALLOC0<imm>╼ │ ╾──────╼
}

ALLOC0 (size: 8, align: 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@
}

ALLOC5 (static: BIG_STAT, size: 4, align: 4) {
╾ALLOC0╼ │ ╾──╼
╾ALLOC0<imm>╼ │ ╾──╼
}

ALLOC0 (size: 20, align: 4) {
0x00 │ 01 00 00 00 23 00 00 00 ╾ALLOC1╼ 02 00 00 00 │ ....#...╾──╼....
0x00 │ 01 00 00 00 23 00 00 00 ╾ALLOC1<imm>╼ 02 00 00 00 │ ....#...╾──╼....
0x10 │ 00 00 a4 42 │ ...B
}

Expand All @@ -233,11 +233,11 @@
}

ALLOC4 (static: SMALL_STAT, size: 4, align: 4) {
╾ALLOC2╼ │ ╾──╼
╾ALLOC2<imm>╼ │ ╾──╼
}

ALLOC2 (size: 20, align: 4) {
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC3╼ 01 00 00 00 │ ....░░░░╾──╼....
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC3<imm>╼ 01 00 00 00 │ ....░░░░╾──╼....
0x10 │ 00 00 10 41 │ ...A
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@
}

ALLOC5 (static: BIG_STAT, size: 8, align: 8) {
╾ALLOC0╼ │ ╾──────╼
╾ALLOC0<imm>╼ │ ╾──────╼
}

ALLOC0 (size: 32, align: 8) {
0x00 │ 01 00 00 00 23 00 00 00 ╾ALLOC1╼ │ ....#...╾──────╼
0x00 │ 01 00 00 00 23 00 00 00 ╾ALLOC1<imm>╼ │ ....#...╾──────╼
0x10 │ 02 00 00 00 00 00 00 00 00 00 a4 42 __ __ __ __ │ ...........B░░░░
}

Expand All @@ -233,11 +233,11 @@
}

ALLOC4 (static: SMALL_STAT, size: 8, align: 8) {
╾ALLOC2╼ │ ╾──────╼
╾ALLOC2<imm>╼ │ ╾──────╼
}

ALLOC2 (size: 32, align: 8) {
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC3╼ │ ....░░░░╾──────╼
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC3<imm>╼ │ ....░░░░╾──────╼
0x10 │ 01 00 00 00 00 00 00 00 00 00 10 41 __ __ __ __ │ ...........A░░░░
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
StorageLive(_6);
_9 = const _;
- _6 = std::alloc::Global::alloc_impl(_9, _1, const false) -> [return: bb4, unwind unreachable];
+ _6 = std::alloc::Global::alloc_impl(const {ALLOC1: &std::alloc::Global}, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): ptr::alignment::AlignmentEnum32) }}, const false) -> [return: bb4, unwind unreachable];
+ _6 = std::alloc::Global::alloc_impl(const {ALLOC1<imm>: &std::alloc::Global}, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): ptr::alignment::AlignmentEnum32) }}, const false) -> [return: bb4, unwind unreachable];
}

bb4: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
StorageLive(_6);
_9 = const _;
- _6 = std::alloc::Global::alloc_impl(_9, _1, const false) -> [return: bb5, unwind continue];
+ _6 = std::alloc::Global::alloc_impl(const {ALLOC1: &std::alloc::Global}, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): ptr::alignment::AlignmentEnum32) }}, const false) -> [return: bb5, unwind continue];
+ _6 = std::alloc::Global::alloc_impl(const {ALLOC1<imm>: &std::alloc::Global}, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): ptr::alignment::AlignmentEnum32) }}, const false) -> [return: bb5, unwind continue];
}

bb5: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
StorageLive(_6);
_9 = const _;
- _6 = std::alloc::Global::alloc_impl(_9, _1, const false) -> [return: bb4, unwind unreachable];
+ _6 = std::alloc::Global::alloc_impl(const {ALLOC1: &std::alloc::Global}, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(8 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x0000000000000000): ptr::alignment::AlignmentEnum64) }}, const false) -> [return: bb4, unwind unreachable];
+ _6 = std::alloc::Global::alloc_impl(const {ALLOC1<imm>: &std::alloc::Global}, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(8 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x0000000000000000): ptr::alignment::AlignmentEnum64) }}, const false) -> [return: bb4, unwind unreachable];
}

bb4: {
Expand Down

0 comments on commit 37a50ac

Please sign in to comment.