@@ -5,7 +5,7 @@ use wasmer_runtime_core::{
55} ;
66
77use crate :: conversion:: to_u32;
8- use crate :: errors:: { VmError , VmResult } ;
8+ use crate :: errors:: { CommunicationError , CommunicationResult , VmError , VmResult } ;
99
1010/****** read/write to wasm memory buffer ****/
1111
@@ -62,7 +62,7 @@ pub fn get_memory_info(ctx: &Ctx) -> MemoryInfo {
6262/// memory region, which is copied in the second step.
6363/// Errors if the length of the region exceeds `max_length`.
6464pub fn read_region ( ctx : & Ctx , ptr : u32 , max_length : usize ) -> VmResult < Vec < u8 > > {
65- let region = get_region ( ctx, ptr) ;
65+ let region = get_region ( ctx, ptr) ? ;
6666
6767 if region. length > to_u32 ( max_length) ? {
6868 return Err ( VmError :: region_length_too_big (
@@ -83,11 +83,11 @@ pub fn read_region(ctx: &Ctx, ptr: u32, max_length: usize) -> VmResult<Vec<u8>>
8383 }
8484 Ok ( result)
8585 }
86- None => panic ! (
87- "Error dereferencing region {:?} in wasm memory of size {}. This typically happens when the given pointer does not point to a Region struct." ,
86+ None => Err ( CommunicationError :: deref_err ( region . offset , format ! (
87+ "Tried to access memory of region {:?} in wasm memory of size {} bytes . This typically happens when the given Region pointer does not point to a proper Region struct." ,
8888 region,
8989 memory. size( ) . bytes( ) . 0
90- ) ,
90+ ) ) . into ( ) ) ,
9191 }
9292}
9393
@@ -106,7 +106,7 @@ pub fn maybe_read_region(ctx: &Ctx, ptr: u32, max_length: usize) -> VmResult<Opt
106106///
107107/// Returns number of bytes written on success.
108108pub fn write_region ( ctx : & Ctx , ptr : u32 , data : & [ u8 ] ) -> VmResult < ( ) > {
109- let mut region = get_region ( ctx, ptr) ;
109+ let mut region = get_region ( ctx, ptr) ? ;
110110
111111 let region_capacity = region. capacity as usize ;
112112 if data. len ( ) > region_capacity {
@@ -122,29 +122,43 @@ pub fn write_region(ctx: &Ctx, ptr: u32, data: &[u8]) -> VmResult<()> {
122122 cells[ i] . set ( data[ i] )
123123 }
124124 region. length = data. len ( ) as u32 ;
125- set_region ( ctx, ptr, region) ;
125+ set_region ( ctx, ptr, region) ? ;
126126 Ok ( ( ) )
127127 } ,
128- None => panic ! (
129- "Error dereferencing region {:?} in wasm memory of size {}. This typically happens when the given pointer does not point to a Region struct." ,
128+ None => Err ( CommunicationError :: deref_err ( region . offset , format ! (
129+ "Tried to access memory of region {:?} in wasm memory of size {} bytes . This typically happens when the given Region pointer does not point to a proper Region struct." ,
130130 region,
131131 memory. size( ) . bytes( ) . 0
132- ) ,
132+ ) ) . into ( ) ) ,
133133 }
134134}
135135
136136/// Reads in a Region at ptr in wasm memory and returns a copy of it
137- fn get_region ( ctx : & Ctx , ptr : u32 ) -> Region {
137+ fn get_region ( ctx : & Ctx , ptr : u32 ) -> CommunicationResult < Region > {
138138 let memory = ctx. memory ( 0 ) ;
139139 let wptr = WasmPtr :: < Region > :: new ( ptr) ;
140- let cell = wptr. deref ( memory) . unwrap ( ) ;
141- cell. get ( )
140+ match wptr. deref ( memory) {
141+ Some ( cell) => Ok ( cell. get ( ) ) ,
142+ None => Err ( CommunicationError :: deref_err (
143+ ptr,
144+ "Could not dereference this pointer to a Region" ,
145+ ) ) ,
146+ }
142147}
143148
144149/// Overrides a Region at ptr in wasm memory with data
145- fn set_region ( ctx : & Ctx , ptr : u32 , data : Region ) {
150+ fn set_region ( ctx : & Ctx , ptr : u32 , data : Region ) -> CommunicationResult < ( ) > {
146151 let memory = ctx. memory ( 0 ) ;
147152 let wptr = WasmPtr :: < Region > :: new ( ptr) ;
148- let cell = wptr. deref ( memory) . unwrap ( ) ;
149- cell. set ( data) ;
153+
154+ match wptr. deref ( memory) {
155+ Some ( cell) => {
156+ cell. set ( data) ;
157+ Ok ( ( ) )
158+ }
159+ None => Err ( CommunicationError :: deref_err (
160+ ptr,
161+ "Could not dereference this pointer to a Region" ,
162+ ) ) ,
163+ }
150164}
0 commit comments