diff options
| -rw-r--r-- | example/test_jit_mips32.py | 7 | ||||
| -rw-r--r-- | example/test_jit_x86_32.py | 1 | ||||
| -rw-r--r-- | miasm2/jitter/Jittcc.c | 38 | ||||
| -rw-r--r-- | miasm2/jitter/jitcore_tcc.py | 7 |
4 files changed, 36 insertions, 17 deletions
diff --git a/example/test_jit_mips32.py b/example/test_jit_mips32.py index 00e7e9a7..2e18b9dd 100644 --- a/example/test_jit_mips32.py +++ b/example/test_jit_mips32.py @@ -33,6 +33,11 @@ parser.add_argument("addr", machine = Machine("mips32l") +def code_sentinelle(jitter): + jitter.run = False + jitter.pc = 0 + return True + def jit_mips32_binary(args): filepath, entryp = args.binary, int(args.addr, 16) myjit = machine.jitter(jit_type = args.jitter) @@ -44,7 +49,7 @@ def jit_mips32_binary(args): myjit.jit.log_newbloc = args.log_newbloc myjit.vm.vm_add_memory_page(0, PAGE_READ | PAGE_WRITE, open(filepath).read()) - myjit.add_breakpoint(0x1337BEEF, lambda _: exit(0)) + myjit.add_breakpoint(0x1337BEEF, code_sentinelle) # for stack diff --git a/example/test_jit_x86_32.py b/example/test_jit_x86_32.py index 977b90fe..44696c74 100644 --- a/example/test_jit_x86_32.py +++ b/example/test_jit_x86_32.py @@ -40,3 +40,4 @@ myjit.add_breakpoint(0x1337beef, code_sentinelle) myjit.init_run(run_addr) myjit.continue_run() +del(myjit) diff --git a/miasm2/jitter/Jittcc.c b/miasm2/jitter/Jittcc.c index fb8b3e27..710a6175 100644 --- a/miasm2/jitter/Jittcc.c +++ b/miasm2/jitter/Jittcc.c @@ -24,10 +24,6 @@ -/* tcc global state */ -TCCState *tcc_state = NULL; - - int include_array_count = 0; char **include_array = NULL; @@ -41,7 +37,7 @@ char **lib_array = NULL; TCCState * tcc_init_state(void) { int i; - + TCCState *tcc_state = NULL; tcc_state = tcc_new(); if (!tcc_state) { fprintf(stderr, "Impossible de creer un contexte TCC\n"); @@ -64,10 +60,11 @@ TCCState * tcc_init_state(void) PyObject* tcc_end(PyObject* self, PyObject* args) { - if (tcc_state) { - tcc_delete(tcc_state); - tcc_state = NULL; - } + TCCState *tcc_state = NULL; + if (!PyArg_ParseTuple(args, "K", &tcc_state)) + return NULL; + tcc_delete(tcc_state); + Py_INCREF(Py_None); return Py_None; } @@ -119,7 +116,6 @@ PyObject* tcc_set_emul_lib_path(PyObject* self, PyObject* args) */ Py_INCREF(Py_None); - tcc_state = tcc_init_state(); return Py_None; } @@ -143,6 +139,10 @@ PyObject* tcc_compil(PyObject* self, PyObject* args) char* func_name; char* func_code; int (*entry)(void); + TCCState *tcc_state = NULL; + PyObject* ret; + + tcc_state = tcc_init_state(); if (!PyArg_ParseTuple(args, "ss", &func_name, &func_code)) return NULL; @@ -150,21 +150,31 @@ PyObject* tcc_compil(PyObject* self, PyObject* args) if (tcc_compile_string(tcc_state, func_code) != 0) { fprintf(stderr, "Erreur de compilation !\n"); fprintf(stderr, "%s\n", func_code); - exit(0); + exit(1); } /* XXX use tinycc devel with -fPIC patch in makefile */ if (tcc_relocate(tcc_state, TCC_RELOCATE_AUTO) < 0) { fprintf(stderr, "tcc relocate error\n"); - exit(0); + exit(1); } entry = tcc_get_symbol(tcc_state, func_name); if (!entry){ fprintf(stderr, "Erreur de symbole %s!\n", func_name); fprintf(stderr, "%s\n", func_name); - exit(0); + exit(1); } - return PyLong_FromUnsignedLongLong((uint64_t)entry); + ret = PyTuple_New(2); + if (ret == NULL) { + fprintf(stderr, "Erreur alloc %s!\n", func_name); + fprintf(stderr, "%s\n", func_name); + exit(1); + } + + PyTuple_SetItem(ret, 0, PyLong_FromUnsignedLongLong((uint64_t)tcc_state)); + PyTuple_SetItem(ret, 1, PyLong_FromUnsignedLongLong((uint64_t)entry)); + + return ret; } diff --git a/miasm2/jitter/jitcore_tcc.py b/miasm2/jitter/jitcore_tcc.py index 36f65f07..cb92361f 100644 --- a/miasm2/jitter/jitcore_tcc.py +++ b/miasm2/jitter/jitcore_tcc.py @@ -94,6 +94,7 @@ class JitCore_Tcc(jitcore.JitCore): super(JitCore_Tcc, self).__init__(my_ir, bs) self.resolver = resolver() self.exec_wrapper = Jittcc.tcc_exec_bloc + self.tcc_states =[] def load(self, arch): # os.path.join(os.path.dirname(os.path.realpath(__file__)), "jitter") @@ -122,7 +123,8 @@ class JitCore_Tcc(jitcore.JitCore): Jittcc.tcc_set_emul_lib_path(include_files, libs) def __del__(self): - Jittcc.tcc_end() + for tcc_state in self.tcc_states: + Jittcc.tcc_end(tcc_state) def jitirblocs(self, label, irblocs): # irbloc = self.lbl2irbloc[lbl] @@ -140,7 +142,8 @@ class JitCore_Tcc(jitcore.JitCore): # print func_code # open('tmp_%.4d.c'%self.jitcount, "w").write(func_code) self.jitcount += 1 - mcode = jit_tcc_compil(f_name, func_code) + tcc_state, mcode = jit_tcc_compil(f_name, func_code) + self.tcc_states.append(tcc_state) jcode = jit_tcc_code(mcode) self.lbl2jitbloc[label.offset] = mcode self.addr2obj[label.offset] = jcode |