Skip to content

Commit 7b97715

Browse files
committed
Always add backtrace to cosmwasm-vm
1 parent ba85804 commit 7b97715

File tree

6 files changed

+94
-134
lines changed

6 files changed

+94
-134
lines changed

packages/vm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ license = "Apache-2.0"
99

1010
[features]
1111
default = ["staking", "iterator"]
12-
# backtraces provides much better context at runtime errors (in non-wasm code)
13-
# at the cost of a bit of code size and performance.
14-
# This feature requires Rust nightly because it depends on the unstable backtrace feature.
15-
backtraces = []
1612
# iterator allows us to iterate over all DB items in a given range
1713
# this must be enabled to support cosmwasm contracts compiled with the 'iterator' feature
1814
# optional as some merkle stores (like tries) don't support this

packages/vm/src/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ mod tests {
12871287
// removing again fails
12881288

12891289
match remove_wasm_from_disk(path, &checksum).unwrap_err() {
1290-
VmError::CacheErr { msg } => assert_eq!(msg, "Wasm file does not exist"),
1290+
VmError::CacheErr { msg, .. } => assert_eq!(msg, "Wasm file does not exist"),
12911291
err => panic!("Unexpected error: {err:?}"),
12921292
}
12931293
}

packages/vm/src/calls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ mod tests {
669669
let msg = br#"{"cpu_loop":{}}"#;
670670
let err =
671671
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap_err();
672-
assert!(matches!(err, VmError::GasDepletion {}));
672+
assert!(matches!(err, VmError::GasDepletion { .. }));
673673
}
674674

675675
#[test]
@@ -687,7 +687,7 @@ mod tests {
687687
let err =
688688
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap_err();
689689
match err {
690-
VmError::RuntimeErr { msg } => {
690+
VmError::RuntimeErr { msg, .. } => {
691691
assert!(msg.contains(
692692
"RuntimeError: Aborted: panicked at 'This page intentionally faulted'"
693693
))
@@ -711,7 +711,7 @@ mod tests {
711711
let err =
712712
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap_err();
713713
match err {
714-
VmError::RuntimeErr { msg } => {
714+
VmError::RuntimeErr { msg, .. } => {
715715
assert!(msg.contains("RuntimeError: unreachable"))
716716
}
717717
err => panic!("Unexpected error: {err:?}"),

packages/vm/src/errors/backtrace.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use core::fmt::{Debug, Display, Formatter, Result};
2+
use std::backtrace::Backtrace;
3+
4+
/// This wraps an actual backtrace to achieve two things:
5+
/// - being able to fill this with a stub implementation in `no_std` environments
6+
/// - being able to use this in conjunction with [`thiserror::Error`]
7+
pub struct BT(Backtrace);
8+
9+
impl BT {
10+
#[track_caller]
11+
pub fn capture() -> Self {
12+
BT(Backtrace::capture())
13+
}
14+
}
15+
16+
impl Debug for BT {
17+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
18+
Debug::fmt(&self.0, f)
19+
}
20+
}
21+
22+
impl Display for BT {
23+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
24+
Display::fmt(&self.0, f)
25+
}
26+
}
27+
28+
/// This macro implements `From` for a given error type to a given error type where
29+
/// the target error has a `backtrace` field.
30+
/// This is meant as a replacement for `thiserror`'s `#[from]` attribute, which does not
31+
/// work with our custom backtrace wrapper.
32+
macro_rules! impl_from_err {
33+
($from:ty, $to:ty, $map:path) => {
34+
impl From<$from> for $to {
35+
fn from(err: $from) -> Self {
36+
$map {
37+
source: err,
38+
backtrace: $crate::errors::backtrace::BT::capture(),
39+
}
40+
}
41+
}
42+
};
43+
}
44+
pub(crate) use impl_from_err;

packages/vm/src/errors/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
mod backtrace;
12
mod communication_error;
23
mod region_validation_error;
34
mod vm_error;
45

6+
pub(crate) use backtrace::{impl_from_err, BT};
57
pub use communication_error::CommunicationError;
68
pub use region_validation_error::RegionValidationError;
79
pub use vm_error::VmError;

0 commit comments

Comments
 (0)