@@ -222,12 +222,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
222222 "__rust_alloc" => {
223223 let size = this. read_scalar ( args[ 0 ] ) ?. to_machine_usize ( this) ?;
224224 let align = this. read_scalar ( args[ 1 ] ) ?. to_machine_usize ( this) ?;
225- if size == 0 {
226- throw_unsup ! ( HeapAllocZeroBytes ) ;
227- }
228- if !align. is_power_of_two ( ) {
229- throw_unsup ! ( HeapAllocNonPowerOfTwoAlignment ( align) ) ;
230- }
225+ Self :: check_alloc_request ( size, align) ?;
231226 let ptr = this. memory . allocate (
232227 Size :: from_bytes ( size) ,
233228 Align :: from_bytes ( align) . unwrap ( ) ,
@@ -238,12 +233,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
238233 "__rust_alloc_zeroed" => {
239234 let size = this. read_scalar ( args[ 0 ] ) ?. to_machine_usize ( this) ?;
240235 let align = this. read_scalar ( args[ 1 ] ) ?. to_machine_usize ( this) ?;
241- if size == 0 {
242- throw_unsup ! ( HeapAllocZeroBytes ) ;
243- }
244- if !align. is_power_of_two ( ) {
245- throw_unsup ! ( HeapAllocNonPowerOfTwoAlignment ( align) ) ;
246- }
236+ Self :: check_alloc_request ( size, align) ?;
247237 let ptr = this. memory . allocate (
248238 Size :: from_bytes ( size) ,
249239 Align :: from_bytes ( align) . unwrap ( ) ,
@@ -257,12 +247,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
257247 let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
258248 let old_size = this. read_scalar ( args[ 1 ] ) ?. to_machine_usize ( this) ?;
259249 let align = this. read_scalar ( args[ 2 ] ) ?. to_machine_usize ( this) ?;
260- if old_size == 0 {
261- throw_unsup ! ( HeapAllocZeroBytes ) ;
262- }
263- if !align. is_power_of_two ( ) {
264- throw_unsup ! ( HeapAllocNonPowerOfTwoAlignment ( align) ) ;
265- }
250+ // No need to check old_size/align; we anyway check that they match the allocation.
266251 let ptr = this. force_ptr ( ptr) ?;
267252 this. memory . deallocate (
268253 ptr,
@@ -274,12 +259,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
274259 let old_size = this. read_scalar ( args[ 1 ] ) ?. to_machine_usize ( this) ?;
275260 let align = this. read_scalar ( args[ 2 ] ) ?. to_machine_usize ( this) ?;
276261 let new_size = this. read_scalar ( args[ 3 ] ) ?. to_machine_usize ( this) ?;
277- if old_size == 0 || new_size == 0 {
278- throw_unsup ! ( HeapAllocZeroBytes ) ;
279- }
280- if !align. is_power_of_two ( ) {
281- throw_unsup ! ( HeapAllocNonPowerOfTwoAlignment ( align) ) ;
282- }
262+ Self :: check_alloc_request ( new_size, align) ?;
263+ // No need to check old_size; we anyway check that they match the allocation.
283264 let ptr = this. force_ptr ( this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?) ?;
284265 let align = Align :: from_bytes ( align) . unwrap ( ) ;
285266 let new_ptr = this. memory . reallocate (
@@ -462,6 +443,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
462443 Ok ( true )
463444 }
464445
446+ /// Check some basic requirements for this allocation request:
447+ /// non-zero size, power-of-two alignment.
448+ fn check_alloc_request ( size : u64 , align : u64 ) -> InterpResult < ' tcx > {
449+ if size == 0 {
450+ throw_ub_format ! ( "creating allocation with size 0" ) ;
451+ }
452+ if !align. is_power_of_two ( ) {
453+ throw_ub_format ! ( "creating allocation with non-power-of-two alignment {}" , align) ;
454+ }
455+ Ok ( ( ) )
456+ }
457+
465458 /// Evaluates the scalar at the specified path. Returns Some(val)
466459 /// if the path could be resolved, and None otherwise
467460 fn eval_path_scalar (
0 commit comments