@@ -40,36 +40,51 @@ use hyperlight_guest::exit::abort_with_code;
40
40
*/
41
41
42
42
// We assume the maximum alignment for any value is the alignment of u128.
43
- const MAX_ALIGN : usize = align_of :: < u128 > ( ) ;
43
+ const DEFAULT_ALIGN : usize = align_of :: < u128 > ( ) ;
44
+ const HEADER_LEN : usize = size_of :: < Header > ( ) ;
45
+
46
+ #[ repr( transparent) ]
47
+ // A header that stores the layout information for the allocated memory block.
48
+ struct Header ( Layout ) ;
44
49
45
50
/// Allocates a block of memory with the given size. The memory is only guaranteed to be initialized to 0s if `zero` is true, otherwise
46
51
/// it may or may not be initialized.
47
52
///
53
+ /// # Invariants
54
+ /// `alignment` must be non-zero and a power of two
55
+ ///
48
56
/// # Safety
49
57
/// The returned pointer must be freed with `memory::free` when it is no longer needed, otherwise memory will leak.
50
- unsafe fn alloc_helper ( size : usize , zero : bool ) -> * mut c_void {
58
+ unsafe fn alloc_helper ( size : usize , alignment : usize , zero : bool ) -> * mut c_void {
51
59
if size == 0 {
52
60
return ptr:: null_mut ( ) ;
53
61
}
54
62
55
- // Allocate a block that includes space for both layout information and data
56
- let total_size = size
57
- . checked_add ( size_of :: < Layout > ( ) )
58
- . expect ( "data and layout size should not overflow in alloc" ) ;
59
- let layout = Layout :: from_size_align ( total_size, MAX_ALIGN ) . expect ( "Invalid layout" ) ;
63
+ let actual_align = alignment. max ( align_of :: < Header > ( ) ) ;
64
+ let data_offset = HEADER_LEN . next_multiple_of ( actual_align) ;
65
+
66
+ let Some ( total_size) = data_offset. checked_add ( size) else {
67
+ abort_with_code ( & [ ErrorCode :: MallocFailed as u8 ] ) ;
68
+ } ;
69
+
70
+ // Create layout for entire allocation
71
+ let layout =
72
+ Layout :: from_size_align ( total_size, actual_align) . expect ( "Invalid layout parameters" ) ;
60
73
61
74
unsafe {
62
75
let raw_ptr = match zero {
63
76
true => alloc:: alloc:: alloc_zeroed ( layout) ,
64
77
false => alloc:: alloc:: alloc ( layout) ,
65
78
} ;
79
+
66
80
if raw_ptr. is_null ( ) {
67
81
abort_with_code ( & [ ErrorCode :: MallocFailed as u8 ] ) ;
68
- } else {
69
- let layout_ptr = raw_ptr as * mut Layout ;
70
- layout_ptr. write ( layout) ;
71
- layout_ptr. add ( 1 ) as * mut c_void
72
82
}
83
+
84
+ // Place Header immediately before the user data region
85
+ let header_ptr = raw_ptr. add ( data_offset - HEADER_LEN ) . cast :: < Header > ( ) ;
86
+ header_ptr. write ( Header ( layout) ) ;
87
+ raw_ptr. add ( data_offset) as * mut c_void
73
88
}
74
89
}
75
90
@@ -80,7 +95,7 @@ unsafe fn alloc_helper(size: usize, zero: bool) -> *mut c_void {
80
95
/// The returned pointer must be freed with `memory::free` when it is no longer needed, otherwise memory will leak.
81
96
#[ unsafe( no_mangle) ]
82
97
pub unsafe extern "C" fn malloc ( size : usize ) -> * mut c_void {
83
- unsafe { alloc_helper ( size, false ) }
98
+ unsafe { alloc_helper ( size, DEFAULT_ALIGN , false ) }
84
99
}
85
100
86
101
/// Allocates a block of memory for an array of `nmemb` elements, each of `size` bytes.
@@ -95,22 +110,45 @@ pub unsafe extern "C" fn calloc(nmemb: usize, size: usize) -> *mut c_void {
95
110
. checked_mul ( size)
96
111
. expect ( "nmemb * size should not overflow in calloc" ) ;
97
112
98
- alloc_helper ( total_size, true )
113
+ alloc_helper ( total_size, DEFAULT_ALIGN , true )
99
114
}
100
115
}
101
116
117
+ /// Allocates aligned memory.
118
+ ///
119
+ /// # Safety
120
+ /// The returned pointer must be freed with `free` when it is no longer needed.
121
+ #[ unsafe( no_mangle) ]
122
+ pub unsafe extern "C" fn aligned_alloc ( alignment : usize , size : usize ) -> * mut c_void {
123
+ // Validate alignment
124
+ if alignment == 0 || ( alignment & ( alignment - 1 ) ) != 0 {
125
+ return ptr:: null_mut ( ) ;
126
+ }
127
+
128
+ unsafe { alloc_helper ( size, alignment, false ) }
129
+ }
130
+
102
131
/// Frees the memory block pointed to by `ptr`.
103
132
///
104
133
/// # Safety
105
134
/// `ptr` must be a pointer to a memory block previously allocated by `memory::malloc`, `memory::calloc`, or `memory::realloc`.
106
135
#[ unsafe( no_mangle) ]
107
136
pub unsafe extern "C" fn free ( ptr : * mut c_void ) {
108
- if !ptr. is_null ( ) {
109
- unsafe {
110
- let block_start = ( ptr as * const Layout ) . sub ( 1 ) ;
111
- let layout = block_start. read ( ) ;
112
- alloc:: alloc:: dealloc ( block_start as * mut u8 , layout)
113
- }
137
+ if ptr. is_null ( ) {
138
+ return ;
139
+ }
140
+
141
+ let user_ptr = ptr as * const u8 ;
142
+
143
+ unsafe {
144
+ // Read the Header just before the user data
145
+ let header_ptr = user_ptr. sub ( HEADER_LEN ) . cast :: < Header > ( ) ;
146
+ let layout = header_ptr. read ( ) . 0 ;
147
+
148
+ // Deallocate from the original base pointer
149
+ let offset = HEADER_LEN . next_multiple_of ( layout. align ( ) ) ;
150
+ let raw_ptr = user_ptr. sub ( offset) as * mut u8 ;
151
+ alloc:: alloc:: dealloc ( raw_ptr, layout) ;
114
152
}
115
153
}
116
154
@@ -134,26 +172,24 @@ pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
134
172
return ptr:: null_mut ( ) ;
135
173
}
136
174
137
- let total_new_size = size
138
- . checked_add ( size_of :: < Layout > ( ) )
139
- . expect ( "data and layout size should not overflow in realloc" ) ;
175
+ let user_ptr = ptr as * const u8 ;
140
176
141
- let block_start = unsafe { ( ptr as * const Layout ) . sub ( 1 ) } ;
142
- let old_layout = unsafe { block_start. read ( ) } ;
143
- let new_layout = Layout :: from_size_align ( total_new_size, MAX_ALIGN ) . unwrap ( ) ;
177
+ unsafe {
178
+ let header_ptr = user_ptr. sub ( HEADER_LEN ) . cast :: < Header > ( ) ;
144
179
145
- let new_block_start =
146
- unsafe { alloc :: alloc :: realloc ( block_start as * mut u8 , old_layout, total_new_size ) }
147
- as * mut Layout ;
180
+ let old_layout = header_ptr . read ( ) . 0 ;
181
+ let old_offset = HEADER_LEN . next_multiple_of ( old_layout. align ( ) ) ;
182
+ let old_user_size = old_layout . size ( ) - old_offset ;
148
183
149
- if new_block_start. is_null ( ) {
150
- // Realloc failed
151
- abort_with_code ( & [ ErrorCode :: MallocFailed as u8 ] ) ;
152
- } else {
153
- // Update the stored Layout, then return ptr to memory right after the Layout.
154
- unsafe {
155
- new_block_start. write ( new_layout) ;
156
- new_block_start. add ( 1 ) as * mut c_void
184
+ let new_ptr = alloc_helper ( size, old_layout. align ( ) , false ) ;
185
+ if new_ptr. is_null ( ) {
186
+ return ptr:: null_mut ( ) ;
157
187
}
188
+
189
+ let copy_size = old_user_size. min ( size) ;
190
+ ptr:: copy_nonoverlapping ( user_ptr, new_ptr as * mut u8 , copy_size) ;
191
+
192
+ free ( ptr) ;
193
+ new_ptr
158
194
}
159
195
}
0 commit comments