Skip to content

Commit bc17165

Browse files
committed
miri: fix offset_from behavior on wildcard pointers
1 parent 579c5b3 commit bc17165

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

tests/fail/intrinsics/ptr_offset_from_different_ints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ fn main() {
1616
let _ = p1.byte_offset_from(p1);
1717

1818
// UB because different pointers.
19-
let _ = p1.byte_offset_from(p2); //~ERROR: different pointers without provenance
19+
let _ = p1.byte_offset_from(p2); //~ERROR: no provenance
2020
}
2121
}

tests/fail/intrinsics/ptr_offset_from_different_ints.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: `ptr_offset_from` called on different pointers without provenance (i.e., without an associated allocation)
1+
error: Undefined Behavior: out-of-bounds `offset_from`: 0xa[noalloc] is a dangling pointer (it has no provenance)
22
--> $DIR/ptr_offset_from_different_ints.rs:LL:CC
33
|
44
LL | let _ = p1.byte_offset_from(p2);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called on different pointers without provenance (i.e., without an associated allocation)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: 0xa[noalloc] is a dangling pointer (it has no provenance)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
//@normalize-stderr-test: "\d+ < \d+" -> "$$ADDR < $$ADDR"
12
#![feature(ptr_sub_ptr)]
23

34
fn main() {
45
let arr = [0u8; 8];
56
let ptr1 = arr.as_ptr();
67
let ptr2 = ptr1.wrapping_add(4);
7-
let _val = unsafe { ptr1.sub_ptr(ptr2) }; //~ERROR: first pointer has smaller offset than second: 0 < 4
8+
let _val = unsafe { ptr1.sub_ptr(ptr2) }; //~ERROR: first pointer has smaller address than second
89
}

tests/fail/intrinsics/ptr_offset_from_unsigned_neg.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: `ptr_offset_from_unsigned` called when first pointer has smaller offset than second: 0 < 4
1+
error: Undefined Behavior: `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
22
--> $DIR/ptr_offset_from_unsigned_neg.rs:LL:CC
33
|
44
LL | let _val = unsafe { ptr1.sub_ptr(ptr2) };
5-
| ^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller offset than second: 0 < 4
5+
| ^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/pass/ptr_int_from_exposed.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@ fn ptr_roundtrip_null() {
5656
assert_eq!(unsafe { *x_ptr_copy }, 42);
5757
}
5858

59+
fn ptr_roundtrip_offset_from() {
60+
let arr = [0; 5];
61+
let begin = arr.as_ptr();
62+
let end = begin.wrapping_add(arr.len());
63+
let end_roundtrip = ptr::with_exposed_provenance::<i32>(end.expose_provenance());
64+
unsafe { end_roundtrip.offset_from(begin) };
65+
}
66+
5967
fn main() {
6068
ptr_roundtrip_out_of_bounds();
6169
ptr_roundtrip_confusion();
6270
ptr_roundtrip_imperfect();
6371
ptr_roundtrip_null();
72+
ptr_roundtrip_offset_from();
6473
}

0 commit comments

Comments
 (0)