@@ -12,7 +12,7 @@ use rustc_target::{
1212 spec:: abi:: Abi ,
1313} ;
1414
15- use super :: alloc:: { check_alloc_request , EvalContextExt as _} ;
15+ use super :: alloc:: EvalContextExt as _;
1616use super :: backtrace:: EvalContextExt as _;
1717use crate :: * ;
1818use helpers:: { ToHost , ToSoft } ;
@@ -204,6 +204,22 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
204204
205205impl < ' tcx > EvalContextExtPriv < ' tcx > for crate :: MiriInterpCx < ' tcx > { }
206206trait EvalContextExtPriv < ' tcx > : crate :: MiriInterpCxExt < ' tcx > {
207+ /// Check some basic requirements for this allocation request:
208+ /// non-zero size, power-of-two alignment.
209+ fn check_rustc_alloc_request ( & self , size : u64 , align : u64 ) -> InterpResult < ' tcx > {
210+ let this = self . eval_context_ref ( ) ;
211+ if size == 0 {
212+ throw_ub_format ! ( "creating allocation with size 0" ) ;
213+ }
214+ if i128:: from ( size) > this. tcx . data_layout . pointer_size . signed_int_max ( ) {
215+ throw_ub_format ! ( "creating an allocation larger than half the address space" ) ;
216+ }
217+ if !align. is_power_of_two ( ) {
218+ throw_ub_format ! ( "creating allocation with non-power-of-two alignment {}" , align) ;
219+ }
220+ Ok ( ( ) )
221+ }
222+
207223 fn emulate_foreign_item_inner (
208224 & mut self ,
209225 link_name : Symbol ,
@@ -462,7 +478,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
462478 let size = this. read_target_usize ( size) ?;
463479 let align = this. read_target_usize ( align) ?;
464480
465- check_alloc_request ( size, align) ?;
481+ this . check_rustc_alloc_request ( size, align) ?;
466482
467483 let memory_kind = match link_name. as_str ( ) {
468484 "__rust_alloc" => MiriMemoryKind :: Rust ,
@@ -496,7 +512,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
496512 let size = this. read_target_usize ( size) ?;
497513 let align = this. read_target_usize ( align) ?;
498514
499- check_alloc_request ( size, align) ?;
515+ this . check_rustc_alloc_request ( size, align) ?;
500516
501517 let ptr = this. allocate_ptr (
502518 Size :: from_bytes ( size) ,
@@ -560,7 +576,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
560576 let new_size = this. read_target_usize ( new_size) ?;
561577 // No need to check old_size; we anyway check that they match the allocation.
562578
563- check_alloc_request ( new_size, align) ?;
579+ this . check_rustc_alloc_request ( new_size, align) ?;
564580
565581 let align = Align :: from_bytes ( align) . unwrap ( ) ;
566582 let new_ptr = this. reallocate_ptr (
0 commit comments