-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create CommunicationError::DerefErr to avoid panics #418
Changes from 5 commits
910b053
b432c50
b2b426d
9bdd795
08e7334
9ec07b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ use wasmer_runtime_core::{ | |
}; | ||
|
||
use crate::conversion::to_u32; | ||
use crate::errors::{VmError, VmResult}; | ||
use crate::errors::{CommunicationError, CommunicationResult, VmError, VmResult}; | ||
|
||
/****** read/write to wasm memory buffer ****/ | ||
|
||
|
@@ -62,7 +62,7 @@ pub fn get_memory_info(ctx: &Ctx) -> MemoryInfo { | |
/// memory region, which is copied in the second step. | ||
/// Errors if the length of the region exceeds `max_length`. | ||
pub fn read_region(ctx: &Ctx, ptr: u32, max_length: usize) -> VmResult<Vec<u8>> { | ||
let region = get_region(ctx, ptr); | ||
let region = get_region(ctx, ptr)?; | ||
|
||
if region.length > to_u32(max_length)? { | ||
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>> | |
} | ||
Ok(result) | ||
} | ||
None => panic!( | ||
None => Err(CommunicationError::deref_err(region.offset, format!( | ||
"Error dereferencing region {:?} in wasm memory of size {}. This typically happens when the given pointer does not point to a Region struct.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need all this text? It will be embedded inside: We can simplify the wording here. (But I like returning this types error over a panic) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can try compress the text slightly, but this text and the debug info are is very helpful. When you read a Region it almost always succeeds, since you can read any 12 bytes into a Region, even if you got completely broken data. Now you try to read the region and get some error. Most likely the deref that returns the error is not the problem but the Region you read before was garbage. E.g. when you should read a 12 MB Region but the Wasm memory is only 1 MB long. Or when you are supposed to read a Region with length > capacity. This is why I add so context here and the helper text. Even a stacktrace is probably way less helpful. "Error dereferencing region" however is wrong since the Region was dereferenced before. It must be "Tried to access memory of region {:?} in wasm memory of size {}. This typically happens when the given Region pointer does not point to a valid Region struct." |
||
region, | ||
memory.size().bytes().0 | ||
), | ||
)).into()), | ||
} | ||
} | ||
|
||
|
@@ -106,7 +106,7 @@ pub fn maybe_read_region(ctx: &Ctx, ptr: u32, max_length: usize) -> VmResult<Opt | |
/// | ||
/// Returns number of bytes written on success. | ||
pub fn write_region(ctx: &Ctx, ptr: u32, data: &[u8]) -> VmResult<()> { | ||
let mut region = get_region(ctx, ptr); | ||
let mut region = get_region(ctx, ptr)?; | ||
|
||
let region_capacity = region.capacity as usize; | ||
if data.len() > region_capacity { | ||
|
@@ -122,29 +122,43 @@ pub fn write_region(ctx: &Ctx, ptr: u32, data: &[u8]) -> VmResult<()> { | |
cells[i].set(data[i]) | ||
} | ||
region.length = data.len() as u32; | ||
set_region(ctx, ptr, region); | ||
set_region(ctx, ptr, region)?; | ||
Ok(()) | ||
}, | ||
None => panic!( | ||
None => Err(CommunicationError::deref_err(region.offset, format!( | ||
"Error dereferencing region {:?} in wasm memory of size {}. This typically happens when the given pointer does not point to a Region struct.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here with verbage |
||
region, | ||
memory.size().bytes().0 | ||
), | ||
)).into()), | ||
} | ||
} | ||
|
||
/// Reads in a Region at ptr in wasm memory and returns a copy of it | ||
fn get_region(ctx: &Ctx, ptr: u32) -> Region { | ||
fn get_region(ctx: &Ctx, ptr: u32) -> CommunicationResult<Region> { | ||
let memory = ctx.memory(0); | ||
let wptr = WasmPtr::<Region>::new(ptr); | ||
let cell = wptr.deref(memory).unwrap(); | ||
cell.get() | ||
match wptr.deref(memory) { | ||
webmaster128 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Some(cell) => Ok(cell.get()), | ||
None => Err(CommunicationError::deref_err( | ||
ptr, | ||
"Could not dereference this pointer to a Region", | ||
)), | ||
} | ||
} | ||
|
||
/// Overrides a Region at ptr in wasm memory with data | ||
fn set_region(ctx: &Ctx, ptr: u32, data: Region) { | ||
fn set_region(ctx: &Ctx, ptr: u32, data: Region) -> CommunicationResult<()> { | ||
let memory = ctx.memory(0); | ||
let wptr = WasmPtr::<Region>::new(ptr); | ||
let cell = wptr.deref(memory).unwrap(); | ||
cell.set(data); | ||
|
||
match wptr.deref(memory) { | ||
Some(cell) => { | ||
cell.set(data); | ||
Ok(()) | ||
} | ||
None => Err(CommunicationError::deref_err( | ||
ptr, | ||
"Could not dereference this pointer to a Region", | ||
)), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this message field?
All the messages written here are basically the same, and basicall say the same thing as the name of the variant plus the offset field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we need it for a lot of important context information in read_region/write_region as described in https://github.com/CosmWasm/cosmwasm/pull/418/files#r438383917. Those two need different context information than the Region deref.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think i see your point. You want to make sure that the pointers are dereferencable in different contexts, and you want to be able to later debug what exactly went wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it is not me debugging this but it is other people who build standard libraries for contract development in other languages that need to get Region handling right. Once this job is done, you hardly see this error again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also #419