Skip to content

Commit c5e8189

Browse files
committed
Auto merge of rust-lang#128742 - RalfJung:miri-vtable-uniqueness, r=saethlin
miri: make vtable addresses not globally unique Miri currently gives vtables a unique global address. That's not actually matching reality though. So this PR enables Miri to generate different addresses for the same type-trait pair. To avoid generating an unbounded number of `AllocId` (and consuming unbounded amounts of memory), we use the "salt" technique that we also already use for giving constants non-unique addresses: the cache is keyed on a "salt" value n top of the actually relevant key, and Miri picks a random salt (currently in the range `0..16`) each time it needs to choose an `AllocId` for one of these globals -- that means we'll get up to 16 different addresses for each vtable. The salt scheme is integrated into the global allocation deduplication logic in `tcx`, and also used for functions and string literals. (So this also fixes the problem that casting the same function to a fn ptr over and over will consume unbounded memory.) r? `@saethlin` Fixes rust-lang/miri#3737
2 parents e3da824 + 048efd0 commit c5e8189

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Diff for: alloc/tests/task.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloc::task::{LocalWake, Wake};
44
use core::task::{LocalWaker, Waker};
55

66
#[test]
7-
#[cfg_attr(miri, should_panic)] // `will_wake` doesn't guarantee that this test will work, and indeed on Miri it fails
7+
#[cfg_attr(miri, ignore)] // `will_wake` doesn't guarantee that this test will work, and indeed on Miri it can fail
88
fn test_waker_will_wake_clone() {
99
struct NoopWaker;
1010

@@ -20,7 +20,7 @@ fn test_waker_will_wake_clone() {
2020
}
2121

2222
#[test]
23-
#[cfg_attr(miri, should_panic)] // `will_wake` doesn't guarantee that this test will work, and indeed on Miri it fails
23+
#[cfg_attr(miri, ignore)] // `will_wake` doesn't guarantee that this test will work, and indeed on Miri it can fail
2424
fn test_local_waker_will_wake_clone() {
2525
struct NoopWaker;
2626

Diff for: core/tests/ptr.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,12 @@ fn ptr_metadata() {
810810
assert_ne!(address_1, address_2);
811811
// Different erased type => different vtable pointer
812812
assert_ne!(address_2, address_3);
813-
// Same erased type and same trait => same vtable pointer
814-
assert_eq!(address_3, address_4);
815-
assert_eq!(address_3, address_5);
813+
// Same erased type and same trait => same vtable pointer.
814+
// This is *not guaranteed*, so we skip it in Miri.
815+
if !cfg!(miri) {
816+
assert_eq!(address_3, address_4);
817+
assert_eq!(address_3, address_5);
818+
}
816819
}
817820
}
818821

0 commit comments

Comments
 (0)