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

Rewrite decode_with for rust api #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 20 additions & 20 deletions sleighcraft/src/cpp/bridge/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,32 @@ unique_ptr<SleighProxy> new_sleigh_proxy(RustLoadImage &ld) {
return proxy;
}

void SleighProxy::decode_with(RustAssemblyEmit& asm_emit, RustPcodeEmit& pcode_emit, uint64_t start) {

int SleighProxy::print_assembly(RustAssemblyEmit& asm_emit, uint64_t start) {
auto assemblyEmit = RustAssemblyEmitProxy{asm_emit};
auto pcodeEmit = RustPcodeEmitProxy{pcode_emit};

Address address(translator.getDefaultCodeSpace(), start);

auto length = 0;
auto buf_used = 0;
auto buf_size = loader.bufSize();

while (buf_used < buf_size) {
try {
length = translator.printAssembly(assemblyEmit, address);
translator.oneInstruction(pcodeEmit, address);
address = address + length;
buf_used = buf_used + length;
try {
auto length = translator.printAssembly(assemblyEmit, address);
return length;
} catch (BadDataError &e) {
throw std::invalid_argument(e.explain);
} catch (UnimplError &e) {
throw std::logic_error(e.explain); // Pcode is not implemented for this constructor
}

} catch (BadDataError &e) {
throw std::invalid_argument("BadDataError");
} catch (UnimplError &e) {
throw std::logic_error("UnimplError"); // Pcode is not implemented for this constructor
}
}

//TODO: implement exception
int SleighProxy::one_instruction(RustPcodeEmit& pcode_emit, uint64_t start) {
auto pcodeEmit = RustPcodeEmitProxy{pcode_emit};
Address address(translator.getDefaultCodeSpace(), start);

try {
auto length = translator.oneInstruction(pcodeEmit, address);
return length;
} catch (BadDataError &e) {
throw std::invalid_argument(e.explain);
} catch (UnimplError &e) {
throw std::logic_error(e.explain); // Pcode is not implemented for this constructor
}

}
Expand Down
3 changes: 2 additions & 1 deletion sleighcraft/src/cpp/bridge/disasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class SleighProxy {

void setSpecFromPath(const rust::Str path, int mode);
void set_spec(const rust::Str spec_content, int mode);
void decode_with(RustAssemblyEmit& asm_emit, RustPcodeEmit& pcode_emit, uint64_t start);
int print_assembly(RustAssemblyEmit& asm_emit, uint64_t start);
int one_instruction(RustPcodeEmit& pcode_emit, uint64_t start);

private:
RustLoadImageProxy loader;
Expand Down
44 changes: 35 additions & 9 deletions sleighcraft/src/sleigh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,8 @@ pub mod ffi {
type SleighProxy;
fn set_spec(self: Pin<&mut SleighProxy>, spec_content: &str, mode: i32);
fn new_sleigh_proxy(ld: &mut RustLoadImage) -> UniquePtr<SleighProxy>;
fn decode_with(
self: Pin<&mut SleighProxy>,
asm_emit: &mut RustAssemblyEmit,
pcode_emit: &mut RustPcodeEmit,
start: u64,
) -> Result<()>;
fn print_assembly(self: Pin<&mut SleighProxy>, asm_emit: &mut RustAssemblyEmit, start: u64) -> Result<i32>;
fn one_instruction(self: Pin<&mut SleighProxy>, pcode_emit: &mut RustPcodeEmit, start: u64) -> Result<i32>;
}
}

Expand Down Expand Up @@ -871,14 +867,44 @@ pub struct Sleigh<'a> {

impl<'a> Sleigh<'a> {
pub fn decode(&mut self, start: u64) -> Result<()> {
// self.load_image.set_buf(bytes);
let buf_size = self._load_image.buf_size();
let mut buf_used = 0;
let mut address = start;
// disassembly
while buf_used < buf_size {
let len = self.print_assembly(address)?;
buf_used += len as usize;
address += len as u64;
}

// get instructions
buf_used = 0;
address = start;

while buf_used < buf_size {
let len = self.one_instruction(address)?;
buf_used += len as usize;
address += len as u64;
}
Ok(())
}

pub fn print_assembly(&mut self, start: u64) -> Result<i32> {
let assembly_emit = self.asm_emit.borrow_mut();
self.sleigh_proxy
.as_mut()
.unwrap()
.print_assembly(assembly_emit, start)
.map_err(|e|Error::CppException(e))
}

pub fn one_instruction(&mut self, start: u64) -> Result<i32> {
let pcodes_emit = self.pcode_emit.borrow_mut();
self.sleigh_proxy
.as_mut()
.unwrap()
.decode_with(assembly_emit, pcodes_emit, start)
.map_err(|e| Error::CppException(e))
.one_instruction(pcodes_emit, start)
.map_err(|e|Error::CppException(e))
}
}

Expand Down