-
Notifications
You must be signed in to change notification settings - Fork 63
Use thiserror for humility-arch-arm
#676
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| use anyhow::{Result, bail}; | ||
| use num_derive::{FromPrimitive, ToPrimitive}; | ||
| use num_traits::ToPrimitive; | ||
| use std::collections::{BTreeMap, HashMap}; | ||
|
|
@@ -211,25 +210,33 @@ fn instr_operands(cs: &Capstone, instr: &capstone::Insn) -> Vec<ARMRegister> { | |
| rval | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum InstructionError { | ||
|
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 want to make this This makes things a little tedious in some places, but we might want to start caring about the impact of semver breaking changes if we expect to start having external consumers. |
||
| #[error("multiple source registers")] | ||
| MultipleSourceRegisters, | ||
|
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. Might be worth including doc comments on the enum, and on each variant, at least answering "what does this error mean, and what should a user do about it if they get it". If I got these errors, I'd probably have to go ping you in chat to figure out what I was doing wrong. If we expect users to start handling these errors (instead of just bailing), we should probably give them some useful feedback! |
||
| #[error("multiple target registers")] | ||
| MultipleTargetRegisters, | ||
| } | ||
|
|
||
| fn instr_source_target( | ||
| cs: &Capstone, | ||
| instr: &capstone::Insn, | ||
| ) -> Result<(Option<ARMRegister>, Option<ARMRegister>)> { | ||
| ) -> Result<(Option<ARMRegister>, Option<ARMRegister>), InstructionError> { | ||
| let detail = cs.insn_detail(instr).unwrap(); | ||
|
|
||
| let mut source: Option<ARMRegister> = None; | ||
| let mut target: Option<ARMRegister> = None; | ||
|
|
||
| for op in detail.regs_read() { | ||
| if source.is_some() { | ||
| bail!("multiple source registers"); | ||
| return Err(InstructionError::MultipleSourceRegisters); | ||
| } | ||
| source = Some((*op).into()); | ||
| } | ||
|
|
||
| for op in detail.regs_write() { | ||
| if target.is_some() { | ||
| bail!("multiple target registers"); | ||
| return Err(InstructionError::MultipleTargetRegisters); | ||
| } | ||
| target = Some((*op).into()); | ||
| } | ||
|
|
@@ -252,7 +259,7 @@ fn instr_source_target( | |
| pub fn presyscall_pushes( | ||
| cs: &Capstone, | ||
| instrs: &[capstone::Insn], | ||
| ) -> Result<Vec<ARMRegister>> { | ||
| ) -> Result<Vec<ARMRegister>, InstructionError> { | ||
| const ARM_INSN_PUSH: u32 = arch::arm::ArmInsn::ARM_INS_PUSH as u32; | ||
| const ARM_INSN_MOV: u32 = arch::arm::ArmInsn::ARM_INS_MOV as u32; | ||
| const ARM_INSN_POP: u32 = arch::arm::ArmInsn::ARM_INS_POP as u32; | ||
|
|
||
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.
Oh, we might also want to hit some common derives here as well, PartialEq, Clone, and Hash are usually big ones.
Clone is just generally useful, PartialEq allows for easier asserting (incl in tests), Hash is also one of those "eventually someone wants to stick it in a map" kind of things.