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

Implement StdError for Error #89

Merged
merged 1 commit into from
Oct 17, 2019
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ byteorder = "1"
bytes = "0.4.12"
goblin = "0.0.24"
ckb-vm-definitions = { path = "definitions", version = "0.17.0" }
derive_more = "0.15.0"

# Feature detection won't work here
[target.'cfg(any(windows, unix))'.dependencies]
Expand Down
44 changes: 44 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::error::Error as StdError;
use std::io::{Error as IOError, ErrorKind};

#[derive(Debug, PartialEq, Clone, Copy, Eq, Display)]
pub enum Error {
#[display(fmt = "parse error")]
ParseError,
#[display(fmt = "unaligned page access")]
Unaligned,
#[display(fmt = "out of bound access")]
OutOfBound,
#[display(fmt = "max cycles exceeded")]
InvalidCycles,
#[display(fmt = "invalid instruction {}", "_0")]
InvalidInstruction(u32),
#[display(fmt = "invalid syscall {}", "_0")]
InvalidEcall(u64),
#[display(fmt = "invalid elf")]
InvalidElfBits,
#[display(fmt = "invalid operand {}", "_0")]
InvalidOp(u8),
#[display(fmt = "I/O error: {:?}", "_0")]
IO(ErrorKind),
#[display(fmt = "dynasm error {}", "_0")]
Dynasm(i32),
#[display(fmt = "assembly error {}", "_0")]
Asm(u8),
#[display(fmt = "limit reached")] // FIXME: Distinguish which limit
LimitReached,
#[display(fmt = "invalid permission")] // FIXME: Distinguish which permission
InvalidPermission,
#[display(fmt = "unexpected error")]
Unexpected,
#[display(fmt = "unimplemented")]
Unimplemented,
}

impl StdError for Error { }

impl From<IOError> for Error {
fn from(error: IOError) -> Self {
Error::IO(error.kind())
}
}
30 changes: 5 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#[macro_use]
extern crate derive_more;

pub mod bits;
pub mod debugger;
pub mod decoder;
pub mod error;
pub mod instructions;
pub mod machine;
pub mod memory;
Expand All @@ -17,37 +21,13 @@ pub use crate::{
syscalls::Syscalls,
};
use bytes::Bytes;
use std::io::{Error as IOError, ErrorKind};

pub use ckb_vm_definitions::{
registers, DEFAULT_STACK_SIZE, RISCV_GENERAL_REGISTER_NUMBER, RISCV_MAX_MEMORY, RISCV_PAGES,
RISCV_PAGESIZE,
};

#[derive(Debug, PartialEq, Clone, Copy, Eq)]
pub enum Error {
ParseError,
Unaligned,
OutOfBound,
InvalidCycles,
InvalidInstruction(u32),
InvalidEcall(u64),
InvalidElfBits,
InvalidOp(u8),
IO(ErrorKind),
Dynasm(i32),
Asm(u8),
LimitReached,
InvalidPermission,
Unexpected,
Unimplemented,
}

impl From<IOError> for Error {
fn from(error: IOError) -> Self {
Error::IO(error.kind())
}
}
pub use error::Error;

pub fn run<R: Register, M: Memory<R> + Default>(
program: &Bytes,
Expand Down