-
Notifications
You must be signed in to change notification settings - Fork 103
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
Improve panic handling code and communication #569
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::any::Any; | ||
|
||
/// A function to process cases in which the VM panics. | ||
/// | ||
/// We want to provide as much debug information as possible | ||
/// as those cases are not expated to happen during healthy operations. | ||
pub fn handle_vm_panic(what: &str, err: Box<dyn Any + Send + 'static>) { | ||
eprintln!("Panic in {what}:"); | ||
eprintln!("{err:?}"); // Does not show useful information, see https://users.rust-lang.org/t/return-value-from-catch-unwind-is-a-useless-any/89134/6 | ||
eprintln!( | ||
"This indicates a panic in during the operations of libwasmvm/cosmwasm-vm. | ||
Such panics must not happen and are considered bugs. If you see this in any real-world or | ||
close-to-real-world usage of wasmvm, please consider filing a security report, | ||
no matter if it can be abused or not: | ||
(https://github.com/CosmWasm/advisories/blob/main/SECURITY.md#reporting-a-vulnerability). | ||
Thank you for your help keeping CosmWasm safe and secure 💚" | ||
); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use std::panic::catch_unwind; | ||
|
||
#[test] | ||
fn handle_vm_panic_works() { | ||
fn nice_try() { | ||
panic!("oh no!"); | ||
} | ||
let err = catch_unwind(nice_try).unwrap_err(); | ||
handle_vm_panic("nice_try", err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe a bit of a nitpick, but I'm not sure how I feel about this test. It's really testing
catch_unwind
rather thanhandle_vm_panic
. Basically the only thing this ensures about our code is thathandle_vm_panic
doesn't panic itself.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 agree for the automated case and it might not be a text book style test. However, it is very handy for manual inspection of the console output (which revealed the
Any
debug problem) as well as ensuring the function can be used in isolation with the weird input type. Do you have an idea how to improve the test?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.
The guard pattern maybe? A bit verbose but no parameter needed?
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.
Well, that code itself won't work immediately because
.with_borrow
isn't enough and it needs a fallback case toio::stdout()
but close enough to illustrate what I mean.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.
And the overhead is alright since it's a rare case to occur anyway (hopefully never)
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.
No strong opinion on if/how to improve this. But if there are no easy wins, I'd merge this as is and leave it up to you to improve it later.
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.
Alright, then let's merge as is for now.