@@ -5,7 +5,7 @@ use wasmer_runtime_core::{
5
5
} ;
6
6
7
7
use crate :: conversion:: to_u32;
8
- use crate :: errors:: { VmError , VmResult } ;
8
+ use crate :: errors:: { CommunicationError , CommunicationResult , VmError , VmResult } ;
9
9
10
10
/****** read/write to wasm memory buffer ****/
11
11
@@ -62,7 +62,7 @@ pub fn get_memory_info(ctx: &Ctx) -> MemoryInfo {
62
62
/// memory region, which is copied in the second step.
63
63
/// Errors if the length of the region exceeds `max_length`.
64
64
pub 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) ? ;
66
66
67
67
if region. length > to_u32 ( max_length) ? {
68
68
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>>
83
83
}
84
84
Ok ( result)
85
85
}
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." ,
88
88
region,
89
89
memory. size( ) . bytes( ) . 0
90
- ) ,
90
+ ) ) . into ( ) ) ,
91
91
}
92
92
}
93
93
@@ -106,7 +106,7 @@ pub fn maybe_read_region(ctx: &Ctx, ptr: u32, max_length: usize) -> VmResult<Opt
106
106
///
107
107
/// Returns number of bytes written on success.
108
108
pub 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) ? ;
110
110
111
111
let region_capacity = region. capacity as usize ;
112
112
if data. len ( ) > region_capacity {
@@ -122,29 +122,43 @@ pub fn write_region(ctx: &Ctx, ptr: u32, data: &[u8]) -> VmResult<()> {
122
122
cells[ i] . set ( data[ i] )
123
123
}
124
124
region. length = data. len ( ) as u32 ;
125
- set_region ( ctx, ptr, region) ;
125
+ set_region ( ctx, ptr, region) ? ;
126
126
Ok ( ( ) )
127
127
} ,
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." ,
130
130
region,
131
131
memory. size( ) . bytes( ) . 0
132
- ) ,
132
+ ) ) . into ( ) ) ,
133
133
}
134
134
}
135
135
136
136
/// 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 > {
138
138
let memory = ctx. memory ( 0 ) ;
139
139
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
+ }
142
147
}
143
148
144
149
/// 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 < ( ) > {
146
151
let memory = ctx. memory ( 0 ) ;
147
152
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
+ }
150
164
}
0 commit comments