Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fff4742

Browse files
committed
Auto merge of rust-lang#2439 - RalfJung:ptr-offset-from-unsigned, r=RalfJung
more tests for ptr_offset_from_unsinged
2 parents a5f0a9b + 8f3b594 commit fff4742

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@error-pattern: first pointer has smaller offset than second: 0 < 4
2+
#![feature(ptr_sub_ptr)]
3+
4+
fn main() {
5+
let arr = [0u8; 8];
6+
let ptr1 = arr.as_ptr();
7+
let ptr2 = ptr1.wrapping_add(4);
8+
let _val = unsafe { ptr1.sub_ptr(ptr2) };
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: ptr_offset_from_unsigned called when first pointer has smaller offset than second: 0 < 4
2+
--> RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
3+
|
4+
LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ptr_offset_from_unsigned called when first pointer has smaller offset than second: 0 < 4
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: backtrace:
10+
= note: inside `std::ptr::const_ptr::<impl *const u8>::sub_ptr` at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC
11+
note: inside `main` at $DIR/ptr_offset_from_unsigned_neg.rs:LL:CC
12+
--> $DIR/ptr_offset_from_unsigned_neg.rs:LL:CC
13+
|
14+
LL | let _val = unsafe { ptr1.sub_ptr(ptr2) };
15+
| ^^^^^^^^^^^^^^^^^^
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to previous error
20+

tests/pass/intrinsics.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@compile-flags: -Zmiri-permissive-provenance
2-
#![feature(core_intrinsics, const_raw_ptr_comparison)]
3-
#![feature(layout_for_ptr)]
2+
#![feature(core_intrinsics, layout_for_ptr)]
3+
//! Tests for various intrinsics that do not fit anywhere else.
44
55
use std::intrinsics;
66
use std::mem::{size_of, size_of_val, size_of_val_raw};
@@ -39,9 +39,4 @@ fn main() {
3939
let _v = intrinsics::discriminant_value(&0);
4040
let _v = intrinsics::discriminant_value(&true);
4141
let _v = intrinsics::discriminant_value(&vec![1, 2, 3]);
42-
43-
let addr = &13 as *const i32;
44-
let addr2 = (addr as usize).wrapping_add(usize::MAX).wrapping_add(1);
45-
assert!(addr.guaranteed_eq(addr2 as *const _));
46-
assert!(addr.guaranteed_ne(0x100 as *const _));
4742
}

tests/pass/pointers.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![feature(ptr_metadata)]
1+
//@compile-flags: -Zmiri-permissive-provenance
2+
#![feature(ptr_metadata, const_raw_ptr_comparison)]
23

34
use std::mem::{self, transmute};
45
use std::ptr;
@@ -131,6 +132,12 @@ fn main() {
131132
assert!(dangling > 3);
132133
assert!(dangling >= 4);
133134

135+
// CTFE-specific equality tests, need to also work at runtime.
136+
let addr = &13 as *const i32;
137+
let addr2 = (addr as usize).wrapping_add(usize::MAX).wrapping_add(1);
138+
assert!(addr.guaranteed_eq(addr2 as *const _));
139+
assert!(addr.guaranteed_ne(0x100 as *const _));
140+
134141
wide_ptr_ops();
135142
metadata_vtable();
136143
}

tests/pass/ptr_offset.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
//@compile-flags: -Zmiri-permissive-provenance
2+
#![feature(ptr_sub_ptr)]
23
use std::{mem, ptr};
34

45
fn main() {
6+
smoke();
57
test_offset_from();
68
test_vec_into_iter();
79
ptr_arith_offset();
810
ptr_arith_offset_overflow();
911
ptr_offset();
1012
}
1113

14+
fn smoke() {
15+
// Smoke-test various offsetting operations.
16+
let ptr = &5;
17+
let ptr = ptr as *const i32;
18+
let _val = ptr.wrapping_offset(0);
19+
let _val = unsafe { ptr.offset(0) };
20+
let _val = ptr.wrapping_add(0);
21+
let _val = unsafe { ptr.add(0) };
22+
let _val = ptr.wrapping_sub(0);
23+
let _val = unsafe { ptr.sub(0) };
24+
let _val = unsafe { ptr.offset_from(ptr) };
25+
let _val = unsafe { ptr.sub_ptr(ptr) };
26+
}
27+
1228
fn test_offset_from() {
1329
unsafe {
1430
let buf = [0u32; 4];
@@ -17,12 +33,14 @@ fn test_offset_from() {
1733
let y = x.offset(12);
1834

1935
assert_eq!(y.offset_from(x), 12);
36+
assert_eq!(y.sub_ptr(x), 12);
2037
assert_eq!(x.offset_from(y), -12);
2138
assert_eq!((y as *const u32).offset_from(x as *const u32), 12 / 4);
2239
assert_eq!((x as *const u32).offset_from(y as *const u32), -12 / 4);
2340

2441
let x = (((x as usize) * 2) / 2) as *const u8;
2542
assert_eq!(y.offset_from(x), 12);
43+
assert_eq!(y.sub_ptr(x), 12);
2644
assert_eq!(x.offset_from(y), -12);
2745
}
2846
}

0 commit comments

Comments
 (0)