@@ -200,11 +200,22 @@ impl SandboxMemoryLayout {
200200 pub ( crate ) fn new (
201201 cfg : SandboxConfiguration ,
202202 code_size : usize ,
203- heap_size : usize ,
204- scratch_size : usize ,
205203 init_data_size : usize ,
206204 init_data_permissions : Option < MemoryRegionFlags > ,
207205 ) -> Result < Self > {
206+ let heap_size = usize:: try_from ( cfg. get_heap_size ( ) ) ?;
207+ let scratch_size = cfg. get_scratch_size ( ) ;
208+ if scratch_size > Self :: MAX_MEMORY_SIZE {
209+ return Err ( MemoryRequestTooBig ( scratch_size, Self :: MAX_MEMORY_SIZE ) ) ;
210+ }
211+ let min_scratch_size = hyperlight_common:: layout:: min_scratch_size (
212+ cfg. get_input_data_size ( ) ,
213+ cfg. get_output_data_size ( ) ,
214+ ) ;
215+ if scratch_size < min_scratch_size {
216+ return Err ( MemoryRequestTooSmall ( scratch_size, min_scratch_size) ) ;
217+ }
218+
208219 let guest_code_offset = 0 ;
209220 // The following offsets are to the fields of the PEB struct itself!
210221 let peb_offset = code_size. next_multiple_of ( PAGE_SIZE_USIZE ) ;
@@ -655,8 +666,7 @@ mod tests {
655666 #[ test]
656667 fn test_get_memory_size ( ) {
657668 let sbox_cfg = SandboxConfiguration :: default ( ) ;
658- let sbox_mem_layout =
659- SandboxMemoryLayout :: new ( sbox_cfg, 4096 , 4096 , 0x3000 , 0 , None ) . unwrap ( ) ;
669+ let sbox_mem_layout = SandboxMemoryLayout :: new ( sbox_cfg, 4096 , 0 , None ) . unwrap ( ) ;
660670 assert_eq ! (
661671 sbox_mem_layout. get_memory_size( ) . unwrap( ) ,
662672 get_expected_memory_size( & sbox_mem_layout)
@@ -667,8 +677,7 @@ mod tests {
667677 fn test_max_memory_sandbox ( ) {
668678 let mut cfg = SandboxConfiguration :: default ( ) ;
669679 cfg. set_input_data_size ( 0x40000000 ) ;
670- let layout = SandboxMemoryLayout :: new ( cfg, 4096 , 2048 , 4096 , 4096 , None ) . unwrap ( ) ;
671- let result = layout. get_memory_size ( ) ;
672- assert ! ( matches!( result. unwrap_err( ) , MemoryRequestTooBig ( ..) ) ) ;
680+ let layout = SandboxMemoryLayout :: new ( cfg, 4096 , 4096 , None ) ;
681+ assert ! ( matches!( layout. unwrap_err( ) , MemoryRequestTooBig ( ..) ) ) ;
673682 }
674683}
0 commit comments