Content-Length: 447302 | pFad | https://github.com/model-checking/verify-rust-std/commit/a48f566b9a4ef050cccde1c685abbe0ed1efc012

E1 offset_from intrinsic: always allow pointers to point to the same add… · model-checking/verify-rust-std@a48f566 · GitHub
Skip to content

Commit a48f566

Browse files
committed
offset_from intrinsic: always allow pointers to point to the same address
1 parent 5569ece commit a48f566

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

core/src/ptr/const_ptr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,9 @@ impl<T: ?Sized> *const T {
604604
//github.com/
605605
//github.com/ * `self` and `origen` must either
606606
//github.com/
607+
//github.com/ * point to the same address, or
607608
//github.com/ * both be *derived from* a pointer to the same [allocated object], and the memory range between
608-
//github.com/ the two pointers must be either empty or in bounds of that object. (See below for an example.)
609-
//github.com/ * or both be derived from an integer literal/constant, and point to the same address.
609+
//github.com/ the two pointers must be in bounds of that object. (See below for an example.)
610610
//github.com/
611611
//github.com/ * The distance between the pointers, in bytes, must be an exact multiple
612612
//github.com/ of the size of `T`.
@@ -653,14 +653,14 @@ impl<T: ?Sized> *const T {
653653
//github.com/ let ptr1 = Box::into_raw(Box::new(0u8)) as *const u8;
654654
//github.com/ let ptr2 = Box::into_raw(Box::new(1u8)) as *const u8;
655655
//github.com/ let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
656-
//github.com/ // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
657-
//github.com/ let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff);
656+
//github.com/ // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
657+
//github.com/ let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff).wrapping_offset(1);
658658
//github.com/ assert_eq!(ptr2 as usize, ptr2_other as usize);
659659
//github.com/ // Since ptr2_other and ptr2 are derived from pointers to different objects,
660660
//github.com/ // computing their offset is undefined behavior, even though
661-
//github.com/ // they point to the same address!
661+
//github.com/ // they point to addresses that are in-bounds of the same object!
662662
//github.com/ unsafe {
663-
//github.com/ let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
663+
//github.com/ let one = ptr2_other.offset_from(ptr2); // Undefined Behavior! ⚠️
664664
//github.com/ }
665665
//github.com/ ```
666666
#[stable(feature = "ptr_offset_from", since = "1.47.0")]

core/src/ptr/mut_ptr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,9 @@ impl<T: ?Sized> *mut T {
829829
//github.com/
830830
//github.com/ * `self` and `origen` must either
831831
//github.com/
832+
//github.com/ * point to the same address, or
832833
//github.com/ * both be *derived from* a pointer to the same [allocated object], and the memory range between
833-
//github.com/ the two pointers must be either empty or in bounds of that object. (See below for an example.)
834-
//github.com/ * or both be derived from an integer literal/constant, and point to the same address.
834+
//github.com/ the two pointers must be in bounds of that object. (See below for an example.)
835835
//github.com/
836836
//github.com/ * The distance between the pointers, in bytes, must be an exact multiple
837837
//github.com/ of the size of `T`.
@@ -878,14 +878,14 @@ impl<T: ?Sized> *mut T {
878878
//github.com/ let ptr1 = Box::into_raw(Box::new(0u8));
879879
//github.com/ let ptr2 = Box::into_raw(Box::new(1u8));
880880
//github.com/ let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
881-
//github.com/ // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
882-
//github.com/ let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff);
881+
//github.com/ // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
882+
//github.com/ let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff).wrapping_offset(1);
883883
//github.com/ assert_eq!(ptr2 as usize, ptr2_other as usize);
884884
//github.com/ // Since ptr2_other and ptr2 are derived from pointers to different objects,
885885
//github.com/ // computing their offset is undefined behavior, even though
886-
//github.com/ // they point to the same address!
886+
//github.com/ // they point to addresses that are in-bounds of the same object!
887887
//github.com/ unsafe {
888-
//github.com/ let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
888+
//github.com/ let one = ptr2_other.offset_from(ptr2); // Undefined Behavior! ⚠️
889889
//github.com/ }
890890
//github.com/ ```
891891
#[stable(feature = "ptr_offset_from", since = "1.47.0")]

core/src/ptr/non_null.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,9 @@ impl<T: ?Sized> NonNull<T> {
735735
//github.com/
736736
//github.com/ * `self` and `origen` must either
737737
//github.com/
738+
//github.com/ * point to the same address, or
738739
//github.com/ * both be *derived from* a pointer to the same [allocated object], and the memory range between
739-
//github.com/ the two pointers must be either empty or in bounds of that object. (See below for an example.)
740-
//github.com/ * or both be derived from an integer literal/constant, and point to the same address.
740+
//github.com/ the two pointers must be in bounds of that object. (See below for an example.)
741741
//github.com/
742742
//github.com/ * The distance between the pointers, in bytes, must be an exact multiple
743743
//github.com/ of the size of `T`.
@@ -789,14 +789,15 @@ impl<T: ?Sized> NonNull<T> {
789789
//github.com/ let ptr1 = NonNull::new(Box::into_raw(Box::new(0u8))).unwrap();
790790
//github.com/ let ptr2 = NonNull::new(Box::into_raw(Box::new(1u8))).unwrap();
791791
//github.com/ let diff = (ptr2.addr().get() as isize).wrapping_sub(ptr1.addr().get() as isize);
792-
//github.com/ // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
793-
//github.com/ let ptr2_other = NonNull::new(ptr1.as_ptr().wrapping_byte_offset(diff)).unwrap();
792+
//github.com/ // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
793+
//github.com/ let diff_plus_1 = diff.wrapping_add(1);
794+
//github.com/ let ptr2_other = NonNull::new(ptr1.as_ptr().wrapping_byte_offset(diff_plus_1)).unwrap();
794795
//github.com/ assert_eq!(ptr2.addr(), ptr2_other.addr());
795796
//github.com/ // Since ptr2_other and ptr2 are derived from pointers to different objects,
796797
//github.com/ // computing their offset is undefined behavior, even though
797-
//github.com/ // they point to the same address!
798+
//github.com/ // they point to addresses that are in-bounds of the same object!
798799
//github.com/
799-
//github.com/ let zero = unsafe { ptr2_other.offset_from(ptr2) }; // Undefined Behavior
800+
//github.com/ let one = unsafe { ptr2_other.offset_from(ptr2) }; // Undefined Behavior! ⚠️
800801
//github.com/ ```
801802
#[inline]
802803
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/model-checking/verify-rust-std/commit/a48f566b9a4ef050cccde1c685abbe0ed1efc012

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy