Commit 45513692 authored by Julian Kranz's avatar Julian Kranz

insncb :-(

parent 1edfb518
......@@ -35,29 +35,28 @@ gdsl::gdsl::gdsl(_frontend *frontend) {
}
gdsl::gdsl::~gdsl() {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("destructor failed", string(frontend->native().generic.get_error_message(gdsl_state)));
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("destructor failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
if(gdsl_state)
frontend->native().generic.destroy(gdsl_state);
if(gdsl_state) frontend->native().generic.destroy(gdsl_state);
}
int_t gdsl::gdsl::get_ip_offset() {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("get_ip_offset() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("get_ip_offset() failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
return frontend->native().generic.get_ip_offset(gdsl_state);
}
void gdsl::gdsl::set_code(unsigned char *buffer, uint64_t size, uint64_t base) {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("set_code() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("set_code() failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
frontend->native().generic.set_code(gdsl_state, buffer, size, base);
}
bool gdsl::gdsl::seek(int_t ip) {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("seek() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("seek() failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
return frontend->native().generic.seek(gdsl_state, ip);
}
......@@ -67,24 +66,24 @@ void gdsl::gdsl::reset_heap() {
}
instruction gdsl::gdsl::decode() {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("decode() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("decode() failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
obj_t native = frontend->native().decoder.decode(gdsl_state, frontend->native().decoder.config_default(gdsl_state));
return instruction(this, native);
}
std::string gdsl::gdsl::pretty_instruction(obj_t insn) {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("pretty_instruction() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("pretty_instruction() failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
obj_t rope = frontend->native().decoder.pretty(gdsl_state, insn);
return std::string(frontend->native().generic.merge_rope(gdsl_state, rope));
}
std::vector<gdsl::rreil::statement*> *gdsl::gdsl::translate(obj_t insn) {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("translate() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("translate() failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
obj_t rreil = frontend->native().translator.translate(gdsl_state, insn);
return convert(rreil);
......@@ -96,20 +95,20 @@ struct gdsl_insns {
};
static obj_t insn_cb(state_t s, obj_t cls, obj_t next) {
gdsl_insns *cls_typed = (gdsl_insns*)cls;
gdsl_insns *cls_typed = (gdsl_insns*) cls;
cls_typed->instructions->push_back(instruction(cls_typed->_this, next));
return cls;
}
block gdsl::gdsl::decode_translate_block(preservation pres, int_t limit) {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("decode_translate_block() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
gdsl_insns cls = {this, new std::vector<instruction>()};
obj_t rreil = frontend->native().translator.decode_translate_block_optimized(gdsl_state,
frontend->native().decoder.config_default(gdsl_state), limit, pres, &cls, insn_cb)->rreil;
obj_t rreil = frontend->native().translator.decode_translate_block_optimized_insncb(gdsl_state,
frontend->native().decoder.config_default(gdsl_state), limit, pres, &cls, insn_cb)->rreil;
std::vector<rreil::statement*> *statements = convert(rreil);
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state))) throw gdsl_exception("decode_translate_block() failed",
string(frontend->native().generic.get_error_message(gdsl_state)));
gdsl_insns cls = { this, new std::vector<instruction>() };
opt_result_t opt_result = frontend->native().translator.decode_translate_block_optimized(gdsl_state,
frontend->native().decoder.config_default(gdsl_state), limit, pres);
frontend->native().translator.traverse_insn_list(gdsl_state, opt_result->insns, &cls, &insn_cb);
std::vector<rreil::statement*> *statements = convert(opt_result->rreil);
return block(cls.instructions, statements);
}
......@@ -51,9 +51,9 @@ struct frontend {
obj_t (*pretty_arch_id)(state_t state, obj_t id);
obj_t (*pretty_arch_exception)(state_t state, obj_t id);
obj_t (*rreil_convert_sem_stmt_list)(state_t s, callbacks_t cbs, obj_t stmts);
opt_result_t (*decode_translate_block_optimized)(state_t state, int_t config, int_t limit, int_t pres,
obj_t insns_init, obj_t (*insn_cb)(state_t, obj_t, obj_t));
obj_t (*traverse_insn_list)(state_t state, obj_t insns_init, obj_t (*insn_cb)(state_t, obj_t, obj_t));
opt_result_t (*decode_translate_block_optimized)(state_t state, int_t config, int_t limit, int_t pres);
obj_t (*traverse_insn_list)(state_t state, obj_t insn_list, obj_t insns_init,
obj_t (*insn_cb)(state_t, obj_t, obj_t));
} translator;
void *dl;
......
......@@ -17,12 +17,12 @@ import java.nio.ByteBuffer;
public class Program {
/**
* @param args
* @throws Throwable
* @throws Throwable
* @throws IOException
*/
private static void sub (ByteBuffer buffer) throws Throwable {
System.out.println("\nsub()\n");
Frontend[] frontends = Gdsl.getFrontends();
for (Frontend frontend : frontends) {
......@@ -32,7 +32,7 @@ public class Program {
Gdsl gdsl = new Gdsl(frontends[0]);
gdsl.setCode(buffer, 0, 0);
Translator t = new Translator(gdsl, new DefaultRReilBuilder());
TranslatedBlock b = t.translateOptimizeBlock(buffer.limit(), SemPres.EVERYWHERE);
......@@ -52,12 +52,14 @@ public class Program {
}
public static void main (String[] args) throws Throwable {
ByteBuffer buffer = ByteBuffer.allocateDirect(4);
ByteBuffer buffer = ByteBuffer.allocateDirect(5);
buffer.put((byte) 0);
buffer.put((byte) 0);
buffer.put((byte) 0);
buffer.put((byte) 0);
// buffer.put((byte) 0xc3);
buffer.put((byte) 0xc3);
sub(buffer);
//
// buffer.put((byte) 0);
// buffer.put((byte) 0);
......@@ -65,16 +67,16 @@ public class Program {
// buffer.put((byte)0x07);
// buffer.put((byte)0x96);
Frontend[] frontends = Gdsl.getFrontends();
Gdsl gdsl = new Gdsl(frontends[0]);
gdsl.setCode(buffer, 0, 0);
Decoder d = new Decoder(gdsl);
NativeInstruction nI = d.decodeOne();
System.out.println(nI.generalize());
System.out.println("+++++++++++++++++++++++++++++");
// Frontend[] frontends = Gdsl.getFrontends();
// Gdsl gdsl = new Gdsl(frontends[0]);
// gdsl.setCode(buffer, 0, 0);
//
// Decoder d = new Decoder(gdsl);
// NativeInstruction nI = d.decodeOne();
//
// System.out.println(nI.generalize());
//
// System.out.println("+++++++++++++++++++++++++++++");
// for (long i = 0; i < 10000000; i++) {
// sub(buffer);
......@@ -100,13 +102,13 @@ public class Program {
// }
// System.out.println("-----");
//
Translator t = new Translator(gdsl, new DefaultRReilBuilder());
IRReilCollection<IStatement> stmts = t.translate(nI);
// Translator t = new Translator(gdsl, new DefaultRReilBuilder());
//
for (int i = 0; i < stmts.size(); i++) {
System.out.println(stmts.get(i));
}
// IRReilCollection<IStatement> stmts = t.translate(nI);
//
// for (int i = 0; i < stmts.size(); i++) {
// System.out.println(stmts.get(i));
// }
//
// gdsl.resetHeap();
// gdsl.destroyFrontend();
......
......@@ -412,15 +412,13 @@ jobject translate_block_optimized_with_config(JNIEnv *env, jobject this, jlong f
coll.size = 0;
opt_result_t opt_result = frontend->translator.decode_translate_block_optimized(state, config, limit, preservation);
frontend->translator.traverse_insn_list(state, &coll, &insn_cb);
frontend->translator.traverse_insn_list(state, opt_result->insns, &coll, &insn_cb);
jlongArray instructions = (*env)->NewLongArray(env, coll.length);
(*env)->SetLongArrayRegion(env, instructions, 0, coll.length, (jlong*) coll.insns);
free(coll.insns);
obj_t rreil = opt_result->rreil;
struct userdata ud;
ud.env = env;
ud.obj = this;
......@@ -429,7 +427,7 @@ jobject translate_block_optimized_with_config(JNIEnv *env, jobject this, jlong f
state->userdata = &ud;
BUILD_CALLBACKS
jobject converted_rreil = frontend->translator.rreil_convert_sem_stmt_list(state, &callbacks, rreil);
jobject converted_rreil = frontend->translator.rreil_convert_sem_stmt_list(state, &callbacks, opt_result->rreil);
jclass TranslatedBlock = (*env)->FindClass(env, "gdsl/translator/TranslatedBlockRaw");
jmethodID TranslatedBlock_ctor = (*env)->GetMethodID(env, TranslatedBlock, "<init>",
......
......@@ -33,7 +33,7 @@ using gdsl::block;
using namespace gdsl::rreil;
struct example_visitor : public statement_visitor {
struct example_visitor: public statement_visitor {
void visit(assign *s) {
printf("Size of assignment: %lld\n", s->get_size());
}
......@@ -45,7 +45,7 @@ struct example_visitor : public statement_visitor {
void demo_single(gdsl::gdsl &g) {
uint16_t buffer = 0x0000;
g.set_code((unsigned char*)&buffer, sizeof(buffer), 0);
g.set_code((unsigned char*) &buffer, sizeof(buffer), 0);
gdsl::instruction insn = g.decode();
......@@ -76,20 +76,20 @@ void demo_single(gdsl::gdsl &g) {
v._([&](assign *a) {
visitor *ev = new visitor();
((linear_visitor*)ev)->_([&](lin_binop *a) {
if(a->get_op() == BIN_LIN_ADD) {
linear_visitor lv;
lv._([&](lin_var *v) {
if(v->get_var()->get_id()->to_string() == "IP") {
ip = true;
if(a->get_op() == BIN_LIN_ADD) {
linear_visitor lv;
lv._([&](lin_var *v) {
if(v->get_var()->get_id()->to_string() == "IP") {
ip = true;
}
});
a->get_opnd1()->accept(lv);
lv._([&](lin_imm *i) {
ip_offset = i->get_imm();
});
a->get_opnd2()->accept(lv);
}
});
a->get_opnd1()->accept(lv);
lv._([&](lin_imm *i) {
ip_offset = i->get_imm();
});
a->get_opnd2()->accept(lv);
}
});
a->accept(*ev);
printf("Size of assignment: %lld\n", a->get_size());
......@@ -109,7 +109,7 @@ void demo_single(gdsl::gdsl &g) {
size_t vars = 0;
for(statement *s : *rreil) {
visitor *v = new visitor();
((statement_visitor*)v)->_([&](assign *a) {
((statement_visitor*) v)->_([&](assign *a) {
printf("Assignment\n");
});
......@@ -129,7 +129,7 @@ void demo_single(gdsl::gdsl &g) {
}
void demo_block(gdsl::gdsl &g) {
uint8_t buffer[] = {0x00, 0x00, 0x00, 0x00, 0xc3};
uint8_t buffer[] = { 0x00, 0x00, 0x00, 0x00, 0xc3 };
g.set_code(buffer, sizeof(buffer), 0);
block b = g.decode_translate_block(gdsl::preservation::BLOCK, LONG_MAX);
......@@ -151,6 +151,8 @@ void demo_block(gdsl::gdsl &g) {
//printf("%s\n", s->to_string().c_str());
std::cout << *s << std::endl;
std::cout << "<<<< RReil output complete" << std::endl;
// Cleanup
for(statement *s : *rreil)
delete s;
......@@ -168,8 +170,7 @@ int main(void) {
printf("\n###############################\n\n");
demo_block(g);
}
catch(std::string &s) {
} catch(std::string &s) {
printf("Exception: %s\n", s.c_str());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment