Skip to content
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

Update derive_more to v1 #440

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ goblin_v040 = { package = "goblin", version = "=0.4.0" }
scroll = "0.10"
serde = { version = "1.0", features = ["derive"] }
ckb-vm-definitions = { path = "definitions", version = "=0.24.0" }
derive_more = "0.99.2"
derive_more = { version = "1", features = ["full"] }
rand = "0.7.3"

[build-dependencies]
Expand Down
53 changes: 23 additions & 30 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,55 @@
#[derive(Debug, PartialEq, Clone, Eq, Display)]
pub enum Error {
#[display(fmt = "asm error: {}", "_0")]
#[display("asm error: {_0}")]
Asm(u8),
#[display(fmt = "cycles error: max cycles exceeded")]
#[display("cycles error: max cycles exceeded")]
CyclesExceeded,
#[display(fmt = "cycles error: overflow")]
#[display("cycles error: overflow")]
CyclesOverflow,
#[display(fmt = "elf error: bits")]
#[display("elf error: bits")]
ElfBits,
#[display(fmt = "elf error: {}", "_0")]
#[display("elf error: {_0}")]
ElfParseError(String),
#[display(fmt = "elf error: segment is unreadable vaddr=0x{:x}", "_0")]
#[display("elf error: segment is unreadable vaddr=0x{_0:x}")]
ElfSegmentUnreadable(u64),
#[display(
fmt = "elf error: segment is writable and executable vaddr=0x{:x}",
"_0"
)]
#[display("elf error: segment is writable and executable vaddr=0x{_0:x}")]
ElfSegmentWritableAndExecutable(u64),
#[display(fmt = "elf error: segment addr or size is wrong vaddr=0x{:x}", "_0")]
#[display("elf error: segment addr or size is wrong vaddr=0x{_0:x}")]
ElfSegmentAddrOrSizeError(u64),
// When users need to implement traits defined in CKB-VM, they can use
// this error type to wrap their own errors.
#[display(fmt = "external error: {}", "_0")]
#[display("external error: {_0}")]
External(String),
#[display(fmt = "invalid syscall {}", "_0")]
#[display("invalid syscall {_0}")]
InvalidEcall(u64),
#[display(
fmt = "invalid instruction pc=0x{:x} instruction=0x{:x}",
"pc",
"instruction"
)]
#[display("invalid instruction pc=0x{pc:x} instruction=0x{instruction:x}")]
InvalidInstruction { pc: u64, instruction: u32 },
#[display(fmt = "invalid operand {}", "_0")]
#[display("invalid operand {_0}")]
InvalidOp(u16),
#[display(fmt = "invalid version")]
#[display("invalid version")]
InvalidVersion,
#[display(fmt = "I/O error: {:?} {}", "kind", "data")]
#[display("I/O error: {kind:?} {data}")]
IO {
kind: std::io::ErrorKind,
data: String,
},
#[display(fmt = "memory error: out of bound addr=0x{:x}, kind={:?}", "_0", "_1")]
#[display("memory error: out of bound addr=0x{_0:x}, kind={_1:?}")]
MemOutOfBound(u64, OutOfBoundKind),
#[display(fmt = "memory error: out of stack")]
#[display("memory error: out of stack")]
MemOutOfStack,
#[display(fmt = "memory error: unaligned page access addr=0x{:x}", "_0")]
#[display("memory error: unaligned page access addr=0x{_0:x}")]
MemPageUnalignedAccess(u64),
#[display(fmt = "memory error: write on executable page page_index={}", "_0")]
#[display("memory error: write on executable page page_index={_0}")]
MemWriteOnExecutablePage(u64),
#[display(fmt = "memory error: write on freezed page page_index={}", "_0")]
#[display("memory error: write on freezed page page_index={_0}")]
MemWriteOnFreezedPage(u64),
#[display(fmt = "pause")]
#[display("pause")]
Pause,
#[display(fmt = "snapshot data load error")]
#[display("snapshot data load error")]
SnapshotDataLoadError,
#[display(fmt = "unexpected error")]
#[display("unexpected error")]
Unexpected(String),
#[display(fmt = "yield")]
#[display("yield")]
Yield,
}

Expand Down
30 changes: 30 additions & 0 deletions tests/test_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ckb_vm::error::Error;

#[test]
pub fn test_error() {
assert_eq!(Error::Asm(0).to_string(), "asm error: 0");
assert_eq!(
Error::ElfParseError(String::from("abcd")).to_string(),
"elf error: abcd"
);
assert_eq!(
Error::ElfSegmentUnreadable(0).to_string(),
"elf error: segment is unreadable vaddr=0x0"
);
assert_eq!(
Error::InvalidInstruction {
pc: 0,
instruction: 1
}
.to_string(),
"invalid instruction pc=0x0 instruction=0x1"
);
assert_eq!(
Error::IO {
kind: std::io::ErrorKind::AddrInUse,
data: String::from("abcd")
}
.to_string(),
"I/O error: AddrInUse abcd"
);
}
Loading