@@ -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 (
@@ -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,7 +122,7 @@ 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 } ,
128128 None => panic ! (
@@ -134,17 +134,29 @@ pub fn write_region(ctx: &Ctx, ptr: u32, data: &[u8]) -> VmResult<()> {
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+ "Could not dereference this pointer to a Region" ,
144+ ) ) ,
145+ }
142146}
143147
144148/// Overrides a Region at ptr in wasm memory with data
145- fn set_region ( ctx : & Ctx , ptr : u32 , data : Region ) {
149+ fn set_region ( ctx : & Ctx , ptr : u32 , data : Region ) -> CommunicationResult < ( ) > {
146150 let memory = ctx. memory ( 0 ) ;
147151 let wptr = WasmPtr :: < Region > :: new ( ptr) ;
148- let cell = wptr. deref ( memory) . unwrap ( ) ;
149- cell. set ( data) ;
152+
153+ match wptr. deref ( memory) {
154+ Some ( cell) => {
155+ cell. set ( data) ;
156+ Ok ( ( ) )
157+ }
158+ None => Err ( CommunicationError :: deref_err (
159+ "Could not dereference this pointer to a Region" ,
160+ ) ) ,
161+ }
150162}
0 commit comments