59
59
use core:: ptr:: NonNull ;
60
60
use core:: sync:: atomic:: { AtomicPtr , Ordering } ;
61
61
use core:: { hint, mem, ptr} ;
62
+ #[ cfg( kani) ]
63
+ use core:: kani;
64
+ use safety:: requires;
62
65
63
66
#[ stable( feature = "alloc_module" , since = "1.28.0" ) ]
64
67
#[ doc( inline) ]
@@ -150,6 +153,10 @@ impl System {
150
153
}
151
154
152
155
// SAFETY: Same as `Allocator::grow`
156
+ #[ requires( new_layout. size( ) >= old_layout. size( ) ) ]
157
+ #[ requires( ptr. as_ptr( ) . is_aligned_to( old_layout. align( ) ) ) ]
158
+ #[ requires( old_layout. size( ) == 0 || old_layout. align( ) != 0 ) ]
159
+ #[ requires( new_layout. size( ) == 0 || new_layout. align( ) != 0 ) ]
153
160
#[ inline]
154
161
unsafe fn grow_impl (
155
162
& self ,
@@ -212,6 +219,7 @@ unsafe impl Allocator for System {
212
219
self . alloc_impl ( layout, true )
213
220
}
214
221
222
+ #[ requires( layout. size( ) != 0 ) ]
215
223
#[ inline]
216
224
unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) {
217
225
if layout. size ( ) != 0 {
@@ -221,6 +229,7 @@ unsafe impl Allocator for System {
221
229
}
222
230
}
223
231
232
+ #[ requires( new_layout. size( ) >= old_layout. size( ) ) ]
224
233
#[ inline]
225
234
unsafe fn grow (
226
235
& self ,
@@ -232,6 +241,7 @@ unsafe impl Allocator for System {
232
241
unsafe { self . grow_impl ( ptr, old_layout, new_layout, false ) }
233
242
}
234
243
244
+ #[ requires( new_layout. size( ) >= old_layout. size( ) ) ]
235
245
#[ inline]
236
246
unsafe fn grow_zeroed (
237
247
& self ,
@@ -243,6 +253,7 @@ unsafe impl Allocator for System {
243
253
unsafe { self . grow_impl ( ptr, old_layout, new_layout, true ) }
244
254
}
245
255
256
+ #[ requires( new_layout. size( ) <= old_layout. size( ) ) ]
246
257
#[ inline]
247
258
unsafe fn shrink (
248
259
& self ,
@@ -382,6 +393,10 @@ pub fn rust_oom(layout: Layout) -> ! {
382
393
#[ allow( unused_attributes) ]
383
394
#[ unstable( feature = "alloc_internals" , issue = "none" ) ]
384
395
pub mod __default_lib_allocator {
396
+ #[ cfg( kani) ]
397
+ use core:: kani;
398
+ use safety:: requires;
399
+
385
400
use super :: { GlobalAlloc , Layout , System } ;
386
401
// These magic symbol names are used as a fallback for implementing the
387
402
// `__rust_alloc` etc symbols (see `src/liballoc/alloc.rs`) when there is
@@ -393,6 +408,7 @@ pub mod __default_lib_allocator {
393
408
// linkage directives are provided as part of the current compiler allocator
394
409
// ABI
395
410
411
+ #[ requires( align. is_power_of_two( ) ) ]
396
412
#[ rustc_std_internal_symbol]
397
413
pub unsafe extern "C" fn __rdl_alloc ( size : usize , align : usize ) -> * mut u8 {
398
414
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
@@ -403,13 +419,15 @@ pub mod __default_lib_allocator {
403
419
}
404
420
}
405
421
422
+ #[ requires( align. is_power_of_two( ) ) ]
406
423
#[ rustc_std_internal_symbol]
407
424
pub unsafe extern "C" fn __rdl_dealloc ( ptr : * mut u8 , size : usize , align : usize ) {
408
425
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
409
426
// `GlobalAlloc::dealloc`.
410
427
unsafe { System . dealloc ( ptr, Layout :: from_size_align_unchecked ( size, align) ) }
411
428
}
412
429
430
+ #[ requires( align. is_power_of_two( ) ) ]
413
431
#[ rustc_std_internal_symbol]
414
432
pub unsafe extern "C" fn __rdl_realloc (
415
433
ptr : * mut u8 ,
@@ -425,6 +443,7 @@ pub mod __default_lib_allocator {
425
443
}
426
444
}
427
445
446
+ #[ requires( align. is_power_of_two( ) ) ]
428
447
#[ rustc_std_internal_symbol]
429
448
pub unsafe extern "C" fn __rdl_alloc_zeroed ( size : usize , align : usize ) -> * mut u8 {
430
449
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
0 commit comments