56
56
#![ deny( unsafe_op_in_unsafe_fn) ]
57
57
#![ stable( feature = "alloc_module" , since = "1.28.0" ) ]
58
58
59
+ #[ cfg( kani) ]
60
+ use core:: kani;
59
61
use core:: ptr:: NonNull ;
60
62
use core:: sync:: atomic:: { Atomic , AtomicPtr , Ordering } ;
61
63
use core:: { hint, mem, ptr} ;
62
64
63
65
#[ stable( feature = "alloc_module" , since = "1.28.0" ) ]
64
66
#[ doc( inline) ]
65
67
pub use alloc_crate:: alloc:: * ;
68
+ use safety:: requires;
66
69
67
70
/// The default memory allocator provided by the operating system.
68
71
///
@@ -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,11 @@ 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
+
399
+ use safety:: requires;
400
+
385
401
use super :: { GlobalAlloc , Layout , System } ;
386
402
// These magic symbol names are used as a fallback for implementing the
387
403
// `__rust_alloc` etc symbols (see `src/liballoc/alloc.rs`) when there is
@@ -393,6 +409,7 @@ pub mod __default_lib_allocator {
393
409
// linkage directives are provided as part of the current compiler allocator
394
410
// ABI
395
411
412
+ #[ requires( align. is_power_of_two( ) ) ]
396
413
#[ rustc_std_internal_symbol]
397
414
pub unsafe extern "C" fn __rdl_alloc ( size : usize , align : usize ) -> * mut u8 {
398
415
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
@@ -403,13 +420,15 @@ pub mod __default_lib_allocator {
403
420
}
404
421
}
405
422
423
+ #[ requires( align. is_power_of_two( ) ) ]
406
424
#[ rustc_std_internal_symbol]
407
425
pub unsafe extern "C" fn __rdl_dealloc ( ptr : * mut u8 , size : usize , align : usize ) {
408
426
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
409
427
// `GlobalAlloc::dealloc`.
410
428
unsafe { System . dealloc ( ptr, Layout :: from_size_align_unchecked ( size, align) ) }
411
429
}
412
430
431
+ #[ requires( align. is_power_of_two( ) ) ]
413
432
#[ rustc_std_internal_symbol]
414
433
pub unsafe extern "C" fn __rdl_realloc (
415
434
ptr : * mut u8 ,
@@ -425,6 +444,7 @@ pub mod __default_lib_allocator {
425
444
}
426
445
}
427
446
447
+ #[ requires( align. is_power_of_two( ) ) ]
428
448
#[ rustc_std_internal_symbol]
429
449
pub unsafe extern "C" fn __rdl_alloc_zeroed ( size : usize , align : usize ) -> * mut u8 {
430
450
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
0 commit comments