We should not fixup the actual object class's vtable, but
the vtable of the class in which method was declared.

This bug led to invocation of incorrect methods.

Signed-off-by: Tomek Grabiec <tgrab...@gmail.com>
---
 arch/x86/emit-code.c |   37 -------------------------------------
 include/jit/vtable.h |    3 +++
 jit/trampoline.c     |    4 ++++
 jit/vtable.c         |   13 +++++++++++++
 4 files changed, 20 insertions(+), 37 deletions(-)

diff --git a/arch/x86/emit-code.c b/arch/x86/emit-code.c
index 91fcf2b..58322f7 100644
--- a/arch/x86/emit-code.c
+++ b/arch/x86/emit-code.c
@@ -433,18 +433,6 @@ void fixup_direct_calls(struct jit_trampoline *t, unsigned 
long target)
        pthread_mutex_unlock(&t->mutex);
 }
 
-/*
- * This function replaces pointers in vtable so that they point
- * directly to compiled code instead of trampoline code.
- */
-static void fixup_vtable(struct compilation_unit *cu,
-       struct vm_object *objref, void *target)
-{
-       struct vm_class *vmc = objref->class;
-
-       vmc->vtable.native_ptr[cu->method->virtual_index] = target;
-}
-
 void fixup_static(struct vm_class *vmc)
 {
        struct static_fixup_site *this, *next;
@@ -1853,18 +1841,6 @@ void emit_trampoline(struct compilation_unit *cu,
                           MACH_REG_ECX);
        __emit_test_membase_reg(buf, MACH_REG_ECX, 0, MACH_REG_ECX);
 
-       __emit_push_reg(buf, MACH_REG_EAX);
-
-       if (method_is_virtual(cu->method)) {
-               __emit_push_membase(buf, MACH_REG_EBP, 0x08);
-
-               __emit_push_imm(buf, (unsigned long)cu);
-               __emit_call(buf, fixup_vtable);
-               __emit_add_imm_reg(buf, 0x08, MACH_REG_ESP);
-       }
-
-       __emit_pop_reg(buf, MACH_REG_EAX);
-
        __emit_pop_reg(buf, MACH_REG_EBP);
        emit_indirect_jump_reg(buf, MACH_REG_EAX);
 
@@ -2833,19 +2809,6 @@ void emit_trampoline(struct compilation_unit *cu,
                           MACH_REG_RCX);
        __emit64_test_membase_reg(buf, MACH_REG_RCX, 0, MACH_REG_RCX);
 
-       if (method_is_virtual(cu->method)) {
-               __emit64_push_reg(buf, MACH_REG_RAX);
-
-               __emit64_mov_imm_reg(buf, (unsigned long) cu, MACH_REG_RDI);
-
-               __emit64_mov_membase_reg(buf, MACH_REG_RBP, 0x10, MACH_REG_RSI);
-
-               __emit64_mov_reg_reg(buf, MACH_REG_RAX, MACH_REG_RDX);
-               __emit_call(buf, fixup_vtable);
-
-               __emit64_pop_reg(buf, MACH_REG_RAX);
-       }
-
        __emit64_pop_reg(buf, MACH_REG_R9);
        __emit64_pop_reg(buf, MACH_REG_R8);
        __emit64_pop_reg(buf, MACH_REG_RCX);
diff --git a/include/jit/vtable.h b/include/jit/vtable.h
index 3d56fa0..0028e04 100644
--- a/include/jit/vtable.h
+++ b/include/jit/vtable.h
@@ -1,6 +1,8 @@
 #ifndef __JIT_VTABLE_H
 #define __JIT_VTABLE_H
 
+struct compilation_unit;
+
 struct vtable {
        void **native_ptr;
 };
@@ -8,5 +10,6 @@ struct vtable {
 void vtable_init(struct vtable *vtable, unsigned int nr_methods);
 void vtable_release(struct vtable *vtable);
 void vtable_setup_method(struct vtable *vtable, unsigned long idx, void 
*native_ptr);
+void fixup_vtable(struct compilation_unit *cu, void *target);
 
 #endif /* __JIT_VTABLE_H */
diff --git a/jit/trampoline.c b/jit/trampoline.c
index 009d514..06036db 100644
--- a/jit/trampoline.c
+++ b/jit/trampoline.c
@@ -144,6 +144,10 @@ void *jit_magic_trampoline(struct compilation_unit *cu)
         * Therefore, do fixup for direct call sites unconditionally and fixup
         * vtables if method can be invoked via invokevirtual.
         */
+
+       if (ret && method_is_virtual(method))
+               fixup_vtable(cu, ret);
+
        pthread_mutex_unlock(&cu->mutex);
 
        /*
diff --git a/jit/vtable.c b/jit/vtable.c
index 6fd9b71..5b8b21b 100644
--- a/jit/vtable.c
+++ b/jit/vtable.c
@@ -25,6 +25,8 @@
  */
 
 #include "jit/vtable.h"
+#include "jit/compilation-unit.h"
+#include "vm/class.h"
 #include <stdlib.h>
 
 void vtable_init(struct vtable *vtable, unsigned int nr_methods)
@@ -41,3 +43,14 @@ void vtable_setup_method(struct vtable *vtable, unsigned 
long idx, void *native_
 {
        vtable->native_ptr[idx] = native_ptr;
 }
+
+/**
+ * This function replaces pointers in vtable so that they point
+ * directly to compiled code instead of trampoline code.
+ */
+void fixup_vtable(struct compilation_unit *cu, void *target)
+{
+       struct vm_class *vmc = cu->method->class;
+
+       vmc->vtable.native_ptr[cu->method->virtual_index] = target;
+}
-- 
1.6.0.6


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to