@@ -13,10 +13,7 @@ pub struct MiriAllocBytes {
1313/// Stored layout information about the allocation.
1414layout : alloc:: Layout ,
1515/// Pointer to the allocation contents.
16- /// Invariant:
17- /// * If `self.layout.size() == 0`, then `self.ptr` is some suitably aligned pointer
18- /// without provenance (and no actual memory was allocated).
19- /// * Otherwise, `self.ptr` points to memory allocated with `self.layout`.
16+ /// Invariant: `self.ptr` points to memory allocated with `self.layout`.
2017ptr : * mut u8 ,
2118}
2219
@@ -30,10 +27,8 @@ impl Clone for MiriAllocBytes {
3027
3128impl Drop for MiriAllocBytes {
3229fn drop ( & mut self ) {
33- if self . layout . size ( ) != 0 {
34- // SAFETY: Invariant, `self.ptr` points to memory allocated with `self.layout`.
35- unsafe { alloc:: dealloc ( self . ptr , self . layout ) }
36- }
30+ // SAFETY: Invariant, `self.ptr` points to memory allocated with `self.layout`.
31+ unsafe { alloc:: dealloc ( self . ptr , self . layout ) }
3732}
3833}
3934
@@ -58,25 +53,23 @@ impl std::ops::DerefMut for MiriAllocBytes {
5853impl MiriAllocBytes {
5954/// This method factors out how a `MiriAllocBytes` object is allocated,
6055/// specifically given an allocation function `alloc_fn`.
61- /// `alloc_fn` is only used if `size != 0`.
62- /// Returns `Err(layout)` if the allocation function returns a `ptr` that is `ptr.is_null()`.
56+ /// `alloc_fn` is only used with `size != 0`.
57+ /// Returns `Err(layout)` if the allocation function returns a `ptr` where `ptr.is_null()`.
6358fn alloc_with (
6459size : usize ,
6560align : usize ,
6661alloc_fn : impl FnOnce ( Layout ) -> * mut u8 ,
6762) -> Result < MiriAllocBytes , Layout > {
63+ // When size is 0 we allocate 1 byte anyway, so addresses don't possibly overlap.
64+ let size = if size == 0 { 1 } else { size } ;
6865let layout = Layout :: from_size_align ( size, align) . unwrap ( ) ;
69- let ptr = if size == 0 {
70- std:: ptr:: without_provenance_mut ( align)
66+ let ptr = alloc_fn ( layout) ;
67+ if ptr. is_null ( ) {
68+ Err ( layout)
7169} else {
72- let ptr = alloc_fn ( layout) ;
73- if ptr. is_null ( ) {
74- return Err ( layout) ;
75- }
76- ptr
77- } ;
78- // SAFETY: All `MiriAllocBytes` invariants are fulfilled.
79- Ok ( Self { ptr, layout } )
70+ // SAFETY: All `MiriAllocBytes` invariants are fulfilled.
71+ Ok ( Self { ptr, layout } )
72+ }
8073}
8174}
8275
@@ -85,7 +78,7 @@ impl AllocBytes for MiriAllocBytes {
8578let slice = slice. into ( ) ;
8679let size = slice. len ( ) ;
8780let align = align. bytes_usize ( ) ;
88- // SAFETY: `alloc_fn` will only be used if `size != 0`.
81+ // SAFETY: `alloc_fn` will only be used with `size != 0`.
8982let alloc_fn = |layout| unsafe { alloc:: alloc ( layout) } ;
9083let alloc_bytes = MiriAllocBytes :: alloc_with ( size, align, alloc_fn)
9184. unwrap_or_else ( |layout| alloc:: handle_alloc_error ( layout) ) ;
@@ -98,7 +91,7 @@ impl AllocBytes for MiriAllocBytes {
9891fn zeroed ( size : Size , align : Align ) -> Option < Self > {
9992let size = size. bytes_usize ( ) ;
10093let align = align. bytes_usize ( ) ;
101- // SAFETY: `alloc_fn` will only be used if `size != 0`.
94+ // SAFETY: `alloc_fn` will only be used with `size != 0`.
10295let alloc_fn = |layout| unsafe { alloc:: alloc_zeroed ( layout) } ;
10396MiriAllocBytes :: alloc_with ( size, align, alloc_fn) . ok ( )
10497}
0 commit comments