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

Regs access #149

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from 4 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
38 changes: 38 additions & 0 deletions capstone-rs/src/capstone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,44 @@ impl Capstone {
}
}

/// Get the registers are which are read to and written to, in that order.
pub fn regs_access(&self, insn: &Insn) -> Option<CsResult<(Vec<RegId>, Vec<RegId>)>> {
joleeee marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like forcing allocations here by returning Vec. You can keep an allocation version for convenience, but please also create a regs_access_buf() where the user can provide their own buffers (which may just be stack allocated).

For simplicity, the regs_access() can call regs_access_buf()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I just append the registers to the provided buffers, or should i clear them first?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user may provide stack allocated buffers, doesn't that mean they generally have to provide the length aswell? If they can pass a regs_read: &mut [u16; MAX_NUM_REGISTERS], how will we specify how many were actually read?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a tuple, please make a new struct that makes it clear what is the read and write fields:

pub struct RegAccess {
    read: Vec<RegId>,
    write: Vec<RegId>,
}

The return type would be CsResult<RegAccess>.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used that struct but made the fields public, is it good now?

if cfg!(feature = "full") {
Some(Ok(unsafe {
let mut regs_read_count: u8 = 0;
let mut regs_write_count: u8 = 0;

let mut regs_write = [0u16; 64];
joleeee marked this conversation as resolved.
Show resolved Hide resolved
let mut regs_read = [0u16; 64];

let err = cs_regs_access(
self.csh(),
&insn.insn as *const cs_insn,
&mut regs_read as *mut _,
&mut regs_read_count as *mut _,
&mut regs_write as *mut _,
&mut regs_write_count as *mut _,
);

if err != cs_err::CS_ERR_OK {
return Some(Err(err.into()));
}

fn to_vec(ints: [u16; 64], len: usize) -> Vec<RegId> {
assert!(len <= ints.len());
joleeee marked this conversation as resolved.
Show resolved Hide resolved
ints[..len].iter().map(|v| RegId(*v)).collect()
}

(
to_vec(regs_read, regs_read_count as usize),
to_vec(regs_write, regs_write_count as usize),
)
}))
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use an early return in the case of an error to reduce the indentation of the "happy" case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you referring to the entire outer if cfg!(...) {? I agree it's cleaner, however the entire file is written this way currently :)

None
}
}

/// Converts a group id `group_id` to a `String` containing the group name.
/// Unavailable in Diet mode
pub fn group_name(&self, group_id: InsnGroupId) -> Option<String> {
Expand Down
Loading