about summary refs log tree commit diff stats
path: root/miasm2/jitter
diff options
context:
space:
mode:
Diffstat (limited to 'miasm2/jitter')
-rw-r--r--miasm2/jitter/codegen.py34
-rw-r--r--miasm2/jitter/jitcore_cc_base.py2
-rw-r--r--miasm2/jitter/jitcore_llvm.py4
-rw-r--r--miasm2/jitter/llvmconvert.py14
-rw-r--r--miasm2/jitter/vm_mngr_py.c8
5 files changed, 41 insertions, 21 deletions
diff --git a/miasm2/jitter/codegen.py b/miasm2/jitter/codegen.py
index 9d005451..09a6fecf 100644
--- a/miasm2/jitter/codegen.py
+++ b/miasm2/jitter/codegen.py
@@ -65,7 +65,7 @@ class CGen(object):
 
     CODE_CPU_EXCEPTION_POST_INSTR = r"""
     if (CPU_exception_flag) {
-        %s = %s;
+        %s = DST_value;
         BlockDst->address = DST_value;
         return JIT_RET_EXCEPTION;
     }
@@ -75,7 +75,7 @@ class CGen(object):
     check_memory_breakpoint(&(jitcpu->pyvm->vm_mngr));
     check_invalid_code_blocs(&(jitcpu->pyvm->vm_mngr));
     if (VM_exception_flag) {
-        %s = %s;
+        %s = DST_value;
         BlockDst->address = DST_value;
         return JIT_RET_EXCEPTION;
     }
@@ -106,6 +106,11 @@ class CGen(object):
 
         self.C_PC = self.id_to_c(self.PC)
 
+    @staticmethod
+    def label_to_jitlabel(lbl):
+        assert lbl.offset is not None
+        return "jitblock_%X" % lbl.offset
+
     def dst_to_c(self, src):
         if not isinstance(src, m2_expr.Expr):
             src = m2_expr.ExprInt(src, self.PC.size)
@@ -296,13 +301,12 @@ class CGen(object):
                 '%s' % ret,
                 '%s' % retb], dst2index
 
-    def gen_post_instr_checks(self, attrib, dst):
+    def gen_post_instr_checks(self, attrib):
         out = []
-        dst = self.dst_to_c(dst)
         if attrib.mem_read | attrib.mem_write:
-            out += (self.CODE_VM_EXCEPTION_POST_INSTR % (self.C_PC, dst)).split('\n')
+            out += (self.CODE_VM_EXCEPTION_POST_INSTR % (self.C_PC)).split('\n')
         if attrib.set_exception or attrib.op_set_exception:
-            out += (self.CODE_CPU_EXCEPTION_POST_INSTR % (self.C_PC, dst)).split('\n')
+            out += (self.CODE_CPU_EXCEPTION_POST_INSTR % (self.C_PC)).split('\n')
 
         if attrib.mem_read | attrib.mem_write:
             out.append("reset_memory_access(&(jitcpu->pyvm->vm_mngr));")
@@ -340,12 +344,12 @@ class CGen(object):
             # (consecutive instructions)
             lbl = self.ir_arch.symbol_pool.getby_offset_create(dst)
             out += self.gen_post_code(attrib)
-            out += self.gen_post_instr_checks(attrib, dst)
-            out.append('goto %s;' % lbl.name)
+            out += self.gen_post_instr_checks(attrib)
+            out.append('goto %s;' % self.label_to_jitlabel(lbl))
         else:
             out += self.gen_post_code(attrib)
             out.append('BlockDst->address = DST_value;')
-            out += self.gen_post_instr_checks(attrib, dst)
+            out += self.gen_post_instr_checks(attrib)
             out.append('\t\treturn JIT_RET_NO_EXCEPTION;')
         return out
 
@@ -497,7 +501,7 @@ class CGen(object):
         instr_offsets = [line.offset for line in block.lines]
         instr_offsets.append(self.get_block_post_label(block).offset)
         lbl_start = self.ir_arch.symbol_pool.getby_offset_create(instr_offsets[0])
-        return (self.CODE_INIT % lbl_start.name).split("\n"), instr_offsets
+        return (self.CODE_INIT % self.label_to_jitlabel(lbl_start)).split("\n"), instr_offsets
 
     def gen_irblock(self, attrib, instr_offsets, instr, irblock):
         """
@@ -535,7 +539,7 @@ class CGen(object):
 
         lbl = self.get_block_post_label(block)
         dst = self.dst_to_c(lbl.offset)
-        code = self.CODE_RETURN_NO_EXCEPTION % (lbl.name, self.C_PC, dst, dst)
+        code = self.CODE_RETURN_NO_EXCEPTION % (self.label_to_jitlabel(lbl), self.C_PC, dst, dst)
         return code.split('\n')
 
     def gen_c(self, block, log_mn=False, log_regs=False):
@@ -558,8 +562,12 @@ class CGen(object):
                 self.ir_arch.irbloc_fix_regs_for_mode(
                     irblock, self.ir_arch.attrib)
 
-                out.append("%-40s // %.16X %s" %
-                           (str(irblock.label.name) + ":", instr.offset, instr))
+                if irblock.label.offset is None:
+                    out.append("%-40s // %.16X %s" %
+                               (str(irblock.label.name) + ":", instr.offset, instr))
+                else:
+                    out.append("%-40s // %.16X %s" %
+                               (self.label_to_jitlabel(irblock.label) + ":", instr.offset, instr))
                 if index == 0:
                     out += self.gen_pre_code(attrib)
                 out += self.gen_irblock(attrib, instr_offsets, instr, irblock)
diff --git a/miasm2/jitter/jitcore_cc_base.py b/miasm2/jitter/jitcore_cc_base.py
index ae8a5dc2..0ca2392d 100644
--- a/miasm2/jitter/jitcore_cc_base.py
+++ b/miasm2/jitter/jitcore_cc_base.py
@@ -90,7 +90,7 @@ class JitCore_Cc_Base(JitCore):
         Generate function name from @label
         @label: AsmLabel instance
         """
-        return "block_%s" % label.name
+        return "block_%s" % self.codegen.label_to_jitlabel(label)
 
     def gen_c_code(self, label, block):
         """
diff --git a/miasm2/jitter/jitcore_llvm.py b/miasm2/jitter/jitcore_llvm.py
index d082dd79..7765ad39 100644
--- a/miasm2/jitter/jitcore_llvm.py
+++ b/miasm2/jitter/jitcore_llvm.py
@@ -82,7 +82,7 @@ class JitCore_LLVM(jitcore.JitCore):
 
         if not os.access(fname_out, os.R_OK):
             # Build a function in the context
-            func = LLVMFunction(self.context, block.label.name)
+            func = LLVMFunction(self.context, LLVMFunction.canonize_label_name(block.label))
 
             # Set log level
             func.log_regs = self.log_regs
@@ -113,7 +113,7 @@ class JitCore_LLVM(jitcore.JitCore):
 
         else:
             # The cache file exists: function can be loaded from cache
-            ptr = self.context.get_ptr_from_cache(fname_out, block.label.name)
+            ptr = self.context.get_ptr_from_cache(fname_out, LLVMFunction.canonize_label_name(block.label))
 
         # Store a pointer on the function jitted code
         self.lbl2jitbloc[block.label.offset] = ptr
diff --git a/miasm2/jitter/llvmconvert.py b/miasm2/jitter/llvmconvert.py
index 85000935..b2e1e957 100644
--- a/miasm2/jitter/llvmconvert.py
+++ b/miasm2/jitter/llvmconvert.py
@@ -503,15 +503,19 @@ class LLVMFunction():
             var_casted = var
         self.builder.ret(var_casted)
 
-    def canonize_label_name(self, label):
+    @staticmethod
+    def canonize_label_name(label):
         """Canonize @label names to a common form.
         @label: str or asmlabel instance"""
         if isinstance(label, str):
             return label
-        elif isinstance(label, m2_asmblock.AsmLabel):
-            return "label_%s" % label.name
-        elif m2_asmblock.expr_is_label(label):
-            return "label_%s" % label.name.name
+        if m2_asmblock.expr_is_label(label):
+            label = label.name
+        if isinstance(label, m2_asmblock.AsmLabel):
+            if label.offset is None:
+                return "label_%s" % label.name
+            else:
+                return "label_%X" % label.offset
         else:
             raise ValueError("label must either be str or asmlabel")
 
diff --git a/miasm2/jitter/vm_mngr_py.c b/miasm2/jitter/vm_mngr_py.c
index e8c8715e..b53e098a 100644
--- a/miasm2/jitter/vm_mngr_py.c
+++ b/miasm2/jitter/vm_mngr_py.c
@@ -262,6 +262,14 @@ PyObject* vm_add_memory_breakpoint(VmMngr* self, PyObject* args)
 	PyGetInt(access, b_access);
 
 	add_memory_breakpoint(&self->vm_mngr, b_ad, b_size, b_access);
+
+	/* Raise exception in the following pattern:
+	   - set_mem(XXX)
+	   - add_memory_breakpoint(XXX)
+	   -> Here, there is a pending breakpoint not raise
+	 */
+	check_memory_breakpoint(&self->vm_mngr);
+
 	Py_INCREF(Py_None);
 	return Py_None;
 }