Skip to content

Commit 36d8f5c

Browse files
committed
Auto merge of #2336 - RalfJung:wide-ptr-compare, r=RalfJung
fix comparing wide raw pointers Fixes rust-lang/rust#96169 However I am not sure if these are the correct semantics. I'll wait for confirmation in that issue.
2 parents 6fcf482 + 6c8ad4a commit 36d8f5c

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/operator.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,19 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
4444
}
4545

4646
Lt | Le | Gt | Ge => {
47-
// Just compare the integers.
48-
let left = left.to_scalar()?.to_bits(left.layout.size)?;
49-
let right = right.to_scalar()?.to_bits(right.layout.size)?;
47+
let size = self.pointer_size();
48+
// Just compare the bits. ScalarPairs are compared lexicographically.
49+
// We thus always compare pairs and simply fill scalars up with 0.
50+
let left = match **left {
51+
Immediate::Scalar(l) => (l.check_init()?.to_bits(size)?, 0),
52+
Immediate::ScalarPair(l1, l2) =>
53+
(l1.check_init()?.to_bits(size)?, l2.check_init()?.to_bits(size)?),
54+
};
55+
let right = match **right {
56+
Immediate::Scalar(r) => (r.check_init()?.to_bits(size)?, 0),
57+
Immediate::ScalarPair(r1, r2) =>
58+
(r1.check_init()?.to_bits(size)?, r2.check_init()?.to_bits(size)?),
59+
};
5060
let res = match bin_op {
5161
Lt => left < right,
5262
Le => left <= right,

tests/pass/pointers.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem::transmute;
2+
13
fn one_line_ref() -> i16 {
24
*&1
35
}
@@ -48,6 +50,27 @@ fn dangling_pointer() -> *const i32 {
4850
&b.0 as *const i32
4951
}
5052

53+
fn wide_ptr_ops() {
54+
let a: *const dyn Send = &1 as &dyn Send;
55+
let b: *const dyn Send = &1 as &dyn Send;
56+
let _val = a == b;
57+
let _val = a != b;
58+
let _val = a < b;
59+
let _val = a <= b;
60+
let _val = a > b;
61+
let _val = a >= b;
62+
63+
let a: *const [u8] = unsafe { transmute((1usize, 1usize)) };
64+
let b: *const [u8] = unsafe { transmute((1usize, 2usize)) };
65+
// confirmed with rustc.
66+
assert!(!(a == b));
67+
assert!(a != b);
68+
assert!(a <= b);
69+
assert!(a < b);
70+
assert!(!(a >= b));
71+
assert!(!(a > b));
72+
}
73+
5174
fn main() {
5275
assert_eq!(one_line_ref(), 1);
5376
assert_eq!(basic_ref(), 1);
@@ -91,4 +114,6 @@ fn main() {
91114
assert!(dangling > 2);
92115
assert!(dangling > 3);
93116
assert!(dangling >= 4);
117+
118+
wide_ptr_ops();
94119
}

0 commit comments

Comments
 (0)