Skip to content

Commit b432c50

Browse files
committed
Propagate deref errors from get_region and set_region
1 parent 910b053 commit b432c50

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

packages/vm/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ impl CommunicationError {
259259
}
260260
}
261261

262+
pub type CommunicationResult<T> = core::result::Result<T, CommunicationError>;
263+
262264
impl From<CommunicationError> for VmError {
263265
fn from(communication_error: CommunicationError) -> Self {
264266
VmError::CommunicationErr {

packages/vm/src/memory.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use wasmer_runtime_core::{
55
};
66

77
use 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`.
6464
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)?;
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.
108108
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)?;
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

Comments
 (0)