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

v0.2.0 api adjustment #30

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion sleighcraft/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sleighcraft"
version = "0.1.1-dev3"
version = "0.2.0-dev1"
authors = ["Anciety <anciety@starcross.cn>"]
edition = "2018"
description = "Binary Analysis Craft"
Expand All @@ -14,6 +14,7 @@ repository = "https://github.com/ret2lab/bincraft/"
cxx = "1.0"
once_cell = "1.6.0"
num_enum = "0.5.1"
downcast-rs = "1.2.0"
sleighcraft_util_macro = { path = "../sleighcraft_util_macro" }

[dependencies.pyo3]
Expand Down
95 changes: 83 additions & 12 deletions sleighcraft/src/cpp/bridge/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,77 @@ void SleighProxy::setSpecFromPath(const rust::Str path,int mode) {
this->ctx.setVariableDefault("opsize",mode); // Operand size is 32-bit
}

unique_ptr<SleighProxy> new_sleigh_proxy(RustLoadImage &ld) {
unique_ptr<SleighProxy> proxy(new SleighProxy(ld));
return proxy;
unique_ptr<SleighProxy> new_sleigh_proxy(rust::Box<RustLoadImage> ld) {
return std::make_unique<SleighProxy>(std::move(ld));
}

void SleighProxy::decode_with(RustAssemblyEmit& asm_emit, RustPcodeEmit& pcode_emit, uint64_t start) {
rust::Box<RustLoadImage>& SleighProxy::get_loader_mut() {
return this->loader->load_image;
}

const rust::Box<RustLoadImage>& SleighProxy::get_loader() const {
return this->loader->load_image;
}

void SleighProxy::set_loader(rust::Box<RustLoadImage> ld) {
this->loader = make_unique<RustLoadImageProxy>(std::move(ld));
this->translator.reset(this->loader.get(), &this->ctx);
this->translator.initialize(storage);
}

rust::Box<RustAssemblyEmit>& SleighProxy::get_asm_emit_mut() {
return this->asm_emit->assemblyEmit;
}

rust::Box<RustPcodeEmit>& SleighProxy::get_pcode_emit_mut() {
return this->pcode_emit->rustPcodeEmit;
}

const rust::Box<RustAssemblyEmit>& SleighProxy::get_asm_emit() const {
return this->asm_emit->assemblyEmit;
}

const rust::Box<RustPcodeEmit>& SleighProxy::get_pcode_emit() const {
return this->pcode_emit->rustPcodeEmit;
}

void SleighProxy::set_asm_emit(rust::Box<RustAssemblyEmit> asm_emit) {
this->asm_emit = std::make_unique<RustAssemblyEmitProxy>(std::move(asm_emit));
}

void SleighProxy::set_pcode_emit(rust::Box<RustPcodeEmit> pcode_emit) {
this->pcode_emit = std::make_unique<RustPcodeEmitProxy>(std::move(pcode_emit));
}

int32_t SleighProxy::decode_asm_at(uint64_t start) {
Address address(translator.getDefaultCodeSpace(), start);

try {
auto length = translator.printAssembly(*asm_emit, address);
return length;
} catch (BadDataError& e) {
throw std::invalid_argument("bad data when decode asm: " + e.explain);
} catch (UnimplError& e) {
throw std::logic_error("pcode not implemented");
}

return 0;
}

void SleighProxy::decode_pcode_at(uint64_t start) {
Address address(translator.getDefaultCodeSpace(), start);

try {
translator.oneInstruction(*pcode_emit, address);
} catch (BadDataError& e) {
throw std::invalid_argument("bad data when decode pcode: " + e.explain);
} catch (UnimplError& e) {
throw std::logic_error("pcode not implemented");
}
}

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

auto assemblyEmit = RustAssemblyEmitProxy{asm_emit};
auto pcodeEmit = RustPcodeEmitProxy{pcode_emit};
Expand All @@ -59,14 +124,19 @@ void SleighProxy::decode_with(RustAssemblyEmit& asm_emit, RustPcodeEmit& pcode_e

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

while (buf_used < buf_size) {
try {
length = translator.printAssembly(assemblyEmit, address);
translator.oneInstruction(pcodeEmit, address);
address = address + length;
buf_used = buf_used + length;
total_insts ++;
if (inst_size > 0 && total_insts >= inst_size) {
break;
}

} catch (BadDataError &e) {
throw std::invalid_argument("BadDataError");
Expand All @@ -79,36 +149,37 @@ void SleighProxy::decode_with(RustAssemblyEmit& asm_emit, RustPcodeEmit& pcode_e
}

}
*/

// RustLoadImageProxy
void RustLoadImageProxy::loadFill(uint1 *ptr, int4 size, const Address &address) {
Address addr = const_cast <Address& > (address);
uint8_t* array = (uint8_t*)ptr;
rust::Slice<::std::uint8_t> slice{array,(unsigned long)size};
const auto addr_proxy = AddressProxy{addr};
load_image.load_fill(slice, addr_proxy);
load_image->load_fill(slice, addr_proxy);
}

void RustLoadImageProxy::adjustVma(long adjust) {
this->load_image.adjust_vma(adjust);
this->load_image->adjust_vma(adjust);
}

string RustLoadImageProxy::getArchType(void) const {
return "plain";
}

int4 RustLoadImageProxy::bufSize() {
return load_image.buf_size();
return load_image->buf_size();
}

std::unique_ptr<RustLoadImageProxy> from_rust(RustLoadImage& load_image) {
return unique_ptr<RustLoadImageProxy>(new RustLoadImageProxy(load_image));
std::unique_ptr<RustLoadImageProxy> from_rust(rust::Box<RustLoadImage> load_image) {
return std::make_unique<RustLoadImageProxy>(std::move(load_image));
}

void RustAssemblyEmitProxy::dump(const Address &address, const string &mnemonic, const string &body) {
Address addr = const_cast <Address& > (address);
const auto addr_proxy = AddressProxy{addr};
assemblyEmit.dump(addr_proxy, mnemonic, body);
assemblyEmit->dump(addr_proxy, mnemonic, body);
}


Expand All @@ -124,5 +195,5 @@ void RustPcodeEmitProxy::dump(const Address &addr, OpCode opc, VarnodeData *outv
vars_vec.push_back(VarnodeDataProxy{&vars[i]});
}

rustPcodeEmit.dump(addr_proxy, opcodes, outvar_proxy, vars_vec);
rustPcodeEmit->dump(addr_proxy, opcodes, outvar_proxy, vars_vec);
}
42 changes: 26 additions & 16 deletions sleighcraft/src/cpp/bridge/disasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ class OpCodeProxy;
class VarnodeDataProxy;
class RustLoadImageProxy: public LoadImage {
public:
RustLoadImage& load_image;
// string(load_image->get_filename())
RustLoadImageProxy(RustLoadImage &load_image): load_image(load_image), LoadImage("nofile") {}

RustLoadImageProxy(RustLoadImage *load_image): load_image(*load_image), LoadImage("nofile") {}
rust::Box<RustLoadImage> load_image;
RustLoadImageProxy(rust::Box<RustLoadImage> load_image): load_image(std::move(load_image)), LoadImage("nofile") {}

virtual void loadFill(uint1 *ptr, int4 size, const Address &address);

Expand Down Expand Up @@ -72,9 +69,8 @@ struct InstructionProxy {

class RustAssemblyEmitProxy: public AssemblyEmit {
public:
RustAssemblyEmit& assemblyEmit;
RustAssemblyEmitProxy(RustAssemblyEmit& assemblyEmit): assemblyEmit(assemblyEmit){}
RustAssemblyEmitProxy(RustAssemblyEmit* assemblyEmit): assemblyEmit(*assemblyEmit){}
rust::Box<RustAssemblyEmit> assemblyEmit;
RustAssemblyEmitProxy(rust::Box<RustAssemblyEmit> assemblyEmit): assemblyEmit(std::move(assemblyEmit)){}

virtual void dump(const Address &address, const string &mnemonic, const string &body);

Expand All @@ -84,32 +80,46 @@ class RustAssemblyEmitProxy: public AssemblyEmit {
class RustPcodeEmitProxy: public PcodeEmit {
public:

RustPcodeEmit& rustPcodeEmit;
RustPcodeEmitProxy(RustPcodeEmit& rustPcodeEmit): rustPcodeEmit(rustPcodeEmit){}
RustPcodeEmitProxy(RustPcodeEmit* rustPcodeEmit): rustPcodeEmit(*rustPcodeEmit){}
rust::Box<RustPcodeEmit> rustPcodeEmit;
RustPcodeEmitProxy(rust::Box<RustPcodeEmit> rustPcodeEmit): rustPcodeEmit(std::move(rustPcodeEmit)){}

virtual void dump(const Address &addr,OpCode opc,VarnodeData *outvar,VarnodeData *vars,int4 isize);

};

class SleighProxy {
public:
SleighProxy(RustLoadImage &ld): loader(ld), translator(&loader, &this->ctx) {}
SleighProxy(rust::Box<RustLoadImage> ld): loader(std::make_unique<RustLoadImageProxy>(std::move(ld))), translator(loader.get(), &this->ctx) {}

void set_asm_emit(rust::Box<RustAssemblyEmit> asm_emit);
void set_pcode_emit(rust::Box<RustPcodeEmit> pcode_emit);
rust::Box<RustAssemblyEmit>& get_asm_emit_mut();
rust::Box<RustPcodeEmit>& get_pcode_emit_mut();
const rust::Box<RustAssemblyEmit>& get_asm_emit() const;
const rust::Box<RustPcodeEmit>& get_pcode_emit() const;

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);
int32_t decode_asm_at(uint64_t start);
void decode_pcode_at(uint64_t start);
//void decode_with(RustAssemblyEmit& asm_emit, RustPcodeEmit& pcode_emit, uint64_t start, uint64_t inst_size);
void set_loader(rust::Box<RustLoadImage> ld);
rust::Box<RustLoadImage>& get_loader_mut();
const rust::Box<RustLoadImage>& get_loader() const;

private:
RustLoadImageProxy loader;
std::unique_ptr<RustLoadImageProxy> loader;
std::unique_ptr<RustAssemblyEmitProxy> asm_emit;
std::unique_ptr<RustPcodeEmitProxy> pcode_emit;

Sleigh translator;
ContextInternal ctx;
DocumentStorage storage;
};

//unique_ptr<SleighProxy> proxy_from_spec(rust::Str path, RustLoadImage &ld, RustAssemblyEmit &asm_emit, RustPcodeEmit &rustPcodeEmit);
//unique_ptr<SleighProxy> proxy_from_spec_path(rust::Str spec_content, RustLoadImage &ld, RustAssemblyEmit &asm_emit, RustPcodeEmit &rustPcodeEmit);
std::unique_ptr<RustLoadImageProxy> from_rust(RustLoadImage& load_image);
unique_ptr<SleighProxy> new_sleigh_proxy(RustLoadImage &ld);
std::unique_ptr<RustLoadImageProxy> from_rust(rust::Box<RustLoadImage> load_image);
unique_ptr<SleighProxy> new_sleigh_proxy(rust::Box<RustLoadImage> ld);

#endif
2 changes: 1 addition & 1 deletion sleighcraft/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub use crate::{arch, CollectingAssemblyEmit, CollectingPcodeEmit, PlainLoadImage, SleighBuilder};
pub use crate::{CollectingAssemblyEmit, CollectingPcodeEmit, PlainLoadImage, Sleigh};
Loading