/* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
- Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
- R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
- C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
- Institut f. Computersprachen - TU Wien
+ Copyright (C) 1996-2005, 2006, 2007, 2008
+ CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
This file is part of CACAO.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- 02111-1307, USA.
-
- Contact: cacao@complang.tuwien.ac.at
-
- Authors: Christian Thalinger
-
- Changes:
-
- $Id: patcher.c 2418 2005-04-29 19:29:17Z twisti $
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
*/
-#include "vm/jit/x86_64/types.h"
-#include "vm/builtin.h"
-#include "vm/field.h"
-#include "vm/initialize.h"
-#include "vm/options.h"
-#include "vm/references.h"
-#include "vm/jit/helper.h"
-#include "vm/exceptions.h"
-
-
-/* patcher_get_putstatic *******************************************************
-
- Machine code:
-
- <patched call position>
- 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
-
-*******************************************************************************/
-
-bool patcher_get_putstatic(u1 *sp)
-{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- unresolved_field *uf;
- fieldinfo *fi;
- s4 offset;
- void *beginJavaStack;
-
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- uf = (unresolved_field *) *((ptrint *) (sp + 0 * 8));
-
- beginJavaStack= (void*)(sp + 3 * 8);
-
- *dontfillinexceptionstacktrace=true;
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
+#include "config.h"
- if (o->vftbl) {
- builtin_monitorexit(o);
+#include <stdint.h>
- return true;
- }
-#endif
+#include "vm/types.h"
- /* get the fieldinfo */
+#include "vm/jit/x86_64/codegen.h"
- if (!(fi = helper_resolve_fieldinfo(uf))) {
- *dontfillinexceptionstacktrace=false;
- return false;
- }
+#include "mm/memory.h"
- /* check if the field's class is initialized */
+#include "native/native.h"
- *dontfillinexceptionstacktrace=false;
- if (!fi->class->initialized) {
- bool init;
- {
- /*struct native_stackframeinfo {
- void *oldThreadspecificHeadValue;
- void **addressOfThreadspecificHead;
- methodinfo *method;
- void *beginOfJavaStackframe; only used if != 0
- void *returnToFromNative;
- }*/
- /* more or less the same as the above sfi setup is done in the assembler code by the prepare/remove functions*/
- native_stackframeinfo sfi;
- sfi.returnToFromNative=(void*)ra;
- sfi.beginOfJavaStackframe=beginJavaStack;
- sfi.method=0; /*internal*/
- sfi.addressOfThreadspecificHead=builtin_asm_get_stackframeinfo();
- sfi.oldThreadspecificHeadValue=*(sfi.addressOfThreadspecificHead);
- *(sfi.addressOfThreadspecificHead)=&sfi;
-
- init=initialize_class(fi->class);
-
- *(sfi.addressOfThreadspecificHead)=sfi.oldThreadspecificHeadValue;
- }
- if (!init)
- {
- return false;
- }
- }
-
- *dontfillinexceptionstacktrace=false;
+#include "vm/builtin.h"
+#include "vm/exceptions.h"
+#include "vm/initialize.h"
- /* patch back original code */
+#include "vm/jit/patcher-common.h"
+#include "vm/jit/stacktrace.h"
- *((u8 *) ra) = mcode;
+#include "vmcore/class.h"
+#include "vmcore/field.h"
+#include "vmcore/options.h"
+#include "vmcore/references.h"
+#include "vm/resolve.h"
- /* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
+#define PATCH_BACK_ORIGINAL_MCODE \
+ do { \
+ *((uint16_t *) pr->mpc) = (uint16_t) pr->mcode; \
+ } while (0)
- /* get RIP offset from machine instruction */
- offset = *((u4 *) (ra + 3));
+/* patcher_patch_code **********************************************************
- /* patch the field value's address (+ 7: is the size of the RIP move) */
+ Just patches back the original machine code.
- *((ptrint *) (ra + 7 + offset)) = (ptrint) &(fi->value);
+*******************************************************************************/
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
+void patcher_patch_code(patchref_t *pr)
+{
+ PATCH_BACK_ORIGINAL_MCODE;
+}
- builtin_monitorexit(o);
- /* this position has been patched */
+/* patcher_resolve_classref_to_classinfo ***************************************
- o->vftbl = (vftbl_t *) 1;
-#endif
+ ACONST:
- return true;
-}
+ <patched call position>
+ 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
+ MULTIANEWARRAY:
-/* patcher_get_putfield ********************************************************
+ <patched call position>
+ 48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
+ 48 89 e2 mov %rsp,%rdx
+ 48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
+ 48 ff d0 callq *%rax
- Machine code:
+ ARRAYCHECKCAST:
<patched call position>
- 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
+ 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
+ 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
+ 48 ff d0 callq *%rax
*******************************************************************************/
-bool patcher_get_putfield(u1 *sp)
+bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- unresolved_field *uf;
- fieldinfo *fi;
-
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- uf = (unresolved_field *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
+ constant_classref *cr;
+ intptr_t *datap;
+ classinfo *c;
- if (o->vftbl) {
- builtin_monitorexit(o);
+ cr = (constant_classref *) pr->ref;
+ datap = (intptr_t *) pr->datap;
- return true;
- }
-#endif
-
- /* get the fieldinfo */
+ /* get the classinfo */
- if (!(fi = helper_resolve_fieldinfo(uf))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(c = resolve_classref_eager(cr)))
return false;
- }
-
- /* patch back original code */
-
- *((u8 *) ra) = mcode;
-
- /* if we show disassembly, we have to skip the nop's */
-
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch the field's offset: we check for the field type, because the */
- /* instructions have different lengths */
-
- if (IS_FLT_DBL_TYPE(fi->type)) {
- *((u4 *) (ra + 5)) = (u4) (fi->offset);
-
- } else {
- u1 byte;
-
- /* check for special case: %rsp or %r12 as base register */
-
- byte = *(ra + 3);
-
- if (byte == 0x24)
- *((u4 *) (ra + 4)) = (u4) (fi->offset);
- else
- *((u4 *) (ra + 3)) = (u4) (fi->offset);
- }
-
- *dontfillinexceptionstacktrace=false;
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
+ PATCH_BACK_ORIGINAL_MCODE;
- builtin_monitorexit(o);
-
- /* this position has been patched */
+ /* patch the classinfo pointer */
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *datap = (intptr_t) c;
return true;
}
-/* patcher_builtin_new *********************************************************
+/* patcher_resolve_classref_to_vftbl *******************************************
- Machine code:
+ CHECKCAST (class):
+ INSTANCEOF (class):
- 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
<patched call position>
- 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
- 48 ff d0 callq *%rax
*******************************************************************************/
-bool patcher_builtin_new(u1 *sp)
+bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
constant_classref *cr;
+ intptr_t *datap;
classinfo *c;
/* get stuff from the stack */
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - (10 + 5);
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
+ cr = (constant_classref *) pr->ref;
+ datap = (intptr_t *) pr->datap;
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* get the classinfo */
+ /* get the fieldinfo */
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(c = resolve_classref_eager(cr)))
return false;
- }
- /* patch back original code */
+ PATCH_BACK_ORIGINAL_MCODE;
- *((u8 *) (ra + 10)) = mcode;
-
- /* patch the classinfo pointer */
-
- *((ptrint *) (ra + 2)) = (ptrint) c;
-
- /* if we show disassembly, we have to skip the nop's */
-
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch new function address */
-
- *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_new;
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
+ /* patch super class' vftbl */
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *datap = (intptr_t) c->vftbl;
return true;
}
-/* patcher_builtin_newarray ****************************************************
+/* patcher_resolve_classref_to_flags *******************************************
- Machine code:
+ CHECKCAST/INSTANCEOF:
- 48 be 88 13 9b 00 00 00 00 00 mov $0x9b1388,%rsi
<patched call position>
- 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
- 48 ff d0 callq *%rax
*******************************************************************************/
-bool patcher_builtin_newarray(u1 *sp)
+bool patcher_resolve_classref_to_flags(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
constant_classref *cr;
+ int32_t *datap;
classinfo *c;
+ uint8_t *ra;
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - (10 + 5);
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
+ cr = (constant_classref *) pr->ref;
+ datap = (int32_t *) pr->datap;
+ ra = (uint8_t *) pr->mpc;
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* get the classinfo */
+ /* get the fieldinfo */
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(c = resolve_classref_eager(cr)))
return false;
- }
-
- /* patch back original code */
- *((u8 *) (ra + 10)) = mcode;
-
- /* patch the class' vftbl pointer */
-
- *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
+ PATCH_BACK_ORIGINAL_MCODE;
/* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch new function address */
+ if (opt_shownops)
+ ra = ra + PATCHER_CALL_SIZE;
- *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_newarray;
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
+ /* patch class flags */
- o->vftbl = (vftbl_t *) 1;
-#endif
+/* *datap = c->flags; */
+ *((int32_t *) (ra + 2)) = c->flags;
return true;
}
-/* patcher_builtin_multianewarray **********************************************
+/* patcher_get_putstatic *******************************************************
Machine code:
<patched call position>
- 48 bf 02 00 00 00 00 00 00 00 mov $0x2,%rdi
- 48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
- 48 89 e2 mov %rsp,%rdx
- 48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
- 48 ff d0 callq *%rax
+ 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
+ 49 8b 32 mov (%r10),%rsi
*******************************************************************************/
-bool patcher_builtin_multianewarray(u1 *sp)
+bool patcher_get_putstatic(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- constant_classref *cr;
- classinfo *c;
-
- /* get stuff from the stack */
+ unresolved_field *uf;
+ intptr_t *datap;
+ fieldinfo *fi;
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
+ uf = (unresolved_field *) pr->ref;
+ datap = (intptr_t *) pr->datap;
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* get the classinfo */
+ /* get the fieldinfo */
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(fi = resolve_field_eager(uf)))
return false;
- }
-
- /* patch back original code */
-
- *((u8 *) ra) = mcode;
- /* if we show disassembly, we have to skip the nop's */
-
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch the class' vftbl pointer */
-
- *((ptrint *) (ra + 10 + 2)) = (ptrint) c->vftbl;
-
- /* patch new function address */
-
- *((ptrint *) (ra + 10 + 10 + 3 + 2)) = (ptrint) BUILTIN_multianewarray;
-
- *dontfillinexceptionstacktrace=false;
+ /* check if the field's class is initialized */
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
+ if (!(fi->clazz->state & CLASS_INITIALIZED))
+ if (!initialize_class(fi->clazz))
+ return false;
- builtin_monitorexit(o);
+ PATCH_BACK_ORIGINAL_MCODE;
- /* this position has been patched */
+ /* patch the field value's address */
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *datap = (intptr_t) fi->value;
return true;
}
-/* patcher_builtin_checkarraycast **********************************************
+/* patcher_get_putfield ********************************************************
Machine code:
- 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
<patched call position>
- 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
- 48 ff d0 callq *%rax
+ 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
*******************************************************************************/
-bool patcher_builtin_checkarraycast(u1 *sp)
+bool patcher_get_putfield(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- constant_classref *cr;
- classinfo *c;
-
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - (10 + 5);
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
+ uint8_t *ra;
+ unresolved_field *uf;
+ fieldinfo *fi;
+ uint8_t byte;
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
+ ra = (uint8_t *) pr->mpc;
+ uf = (unresolved_field *) pr->ref;
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* get the classinfo */
+ /* get the fieldinfo */
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(fi = resolve_field_eager(uf)))
return false;
- }
-
- /* patch back original code */
-
- *((u8 *) (ra + 10)) = mcode;
-
- /* patch the class' vftbl pointer */
- *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
+ PATCH_BACK_ORIGINAL_MCODE;
/* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
+ if (opt_shownops)
+ ra = ra + PATCHER_CALL_SIZE;
- /* patch new function address */
+ /* Patch the field's offset: we check for the field type, because
+ the instructions have different lengths. */
- *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_checkarraycast;
+ if (IS_INT_LNG_TYPE(fi->type)) {
+ /* Check for special case: %rsp or %r12 as base register. */
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
+ byte = *(ra + 3);
- builtin_monitorexit(o);
+ if (byte == 0x24)
+ *((int32_t *) (ra + 4)) = fi->offset;
+ else
+ *((int32_t *) (ra + 3)) = fi->offset;
+ }
+ else {
+ /* Check for special case: %rsp or %r12 as base register. */
- /* this position has been patched */
+ byte = *(ra + 5);
- o->vftbl = (vftbl_t *) 1;
-#endif
+ if (byte == 0x24)
+ *((int32_t *) (ra + 6)) = fi->offset;
+ else
+ *((int32_t *) (ra + 5)) = fi->offset;
+ }
return true;
}
-/* patcher_builtin_arrayinstanceof *********************************************
+/* patcher_putfieldconst *******************************************************
Machine code:
- 48 be 30 3c b2 00 00 00 00 00 mov $0xb23c30,%rsi
<patched call position>
- 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
- 48 ff d0 callq *%rax
+ 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
*******************************************************************************/
-bool patcher_builtin_arrayinstanceof(u1 *sp)
+bool patcher_putfieldconst(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- constant_classref *cr;
- classinfo *c;
-
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - (10 + 5);
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
+ uint8_t *ra;
+ unresolved_field *uf;
+ fieldinfo *fi;
+ uint8_t byte;
- *dontfillinexceptionstacktrace=true;
+ ra = (uint8_t *) pr->mpc;
+ uf = (unresolved_field *) pr->ref;
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* get the classinfo */
+ /* get the fieldinfo */
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(fi = resolve_field_eager(uf)))
return false;
- }
-
- /* patch back original code */
-
- *((u8 *) (ra + 10)) = mcode;
- /* patch the class' vftbl pointer */
-
- *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
+ PATCH_BACK_ORIGINAL_MCODE;
/* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch new function address */
+ if (opt_shownops)
+ ra = ra + PATCHER_CALL_SIZE;
- *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_arrayinstanceof;
+ /* patch the field's offset */
- *dontfillinexceptionstacktrace=false;
+ if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
+ /* handle special case when the base register is %r12 */
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
+ byte = *(ra + 12);
- builtin_monitorexit(o);
+ if (byte == 0x94) {
+ *((uint32_t *) (ra + 14)) = fi->offset;
+ }
+ else {
+ *((uint32_t *) (ra + 13)) = fi->offset;
+ }
+ }
+ else {
+ /* handle special case when the base register is %r12 */
- /* this position has been patched */
+ byte = *(ra + 2);
- o->vftbl = (vftbl_t *) 1;
-#endif
+ if (byte == 0x84)
+ *((uint32_t *) (ra + 4)) = fi->offset;
+ else
+ *((uint32_t *) (ra + 3)) = fi->offset;
+ }
return true;
}
/* patcher_invokestatic_special ************************************************
- XXX
+ Machine code:
+
+ <patched call position>
+ 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
+ 49 ff d2 callq *%r10
*******************************************************************************/
-bool patcher_invokestatic_special(u1 *sp)
+bool patcher_invokestatic_special(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
unresolved_method *um;
+ intptr_t *datap;
methodinfo *m;
/* get stuff from the stack */
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- um = (unresolved_method *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
+ um = (unresolved_method *) pr->ref;
+ datap = (intptr_t *) pr->datap;
/* get the fieldinfo */
- if (!(m = helper_resolve_methodinfo(um))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(m = resolve_method_eager(um)))
return false;
- }
- /* patch back original code */
- *((u8 *) ra) = mcode;
-
- /* if we show disassembly, we have to skip the nop's */
-
- if (showdisassemble)
- ra = ra + 5;
+ PATCH_BACK_ORIGINAL_MCODE;
/* patch stubroutine */
- *((ptrint *) (ra + 2)) = (ptrint) m->stubroutine;
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
-
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *datap = (intptr_t) m->stubroutine;
return true;
}
/* patcher_invokevirtual *******************************************************
- XXX
+ Machine code:
+
+ <patched call position>
+ 4c 8b 17 mov (%rdi),%r10
+ 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
+ 48 ff d0 callq *%rax
*******************************************************************************/
-bool patcher_invokevirtual(u1 *sp)
+bool patcher_invokevirtual(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
+ uint8_t *ra;
unresolved_method *um;
methodinfo *m;
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- um = (unresolved_method *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
+ ra = (uint8_t *) pr->mpc;
+ um = (unresolved_method *) pr->ref;
- if (o->vftbl) {
- builtin_monitorexit(o);
+ /* get the methodinfo */
- return true;
- }
-#endif
-
- /* get the fieldinfo */
-
- if (!(m = helper_resolve_methodinfo(um))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(m = resolve_method_eager(um)))
return false;
- }
-
- /* patch back original code */
- *((u8 *) ra) = mcode;
+ PATCH_BACK_ORIGINAL_MCODE;
/* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
+ if (opt_shownops)
+ ra = ra + PATCHER_CALL_SIZE;
/* patch vftbl index */
- *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, table[0]) +
- sizeof(methodptr) * m->vftblindex);
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
-
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *((int32_t *) (ra + 3 + 3)) =
+ (int32_t) (OFFSET(vftbl_t, table[0]) +
+ sizeof(methodptr) * m->vftblindex);
return true;
}
/* patcher_invokeinterface *****************************************************
- XXX
+ Machine code:
+
+ <patched call position>
+ 4c 8b 17 mov (%rdi),%r10
+ 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
+ 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
+ 48 ff d0 callq *%rax
*******************************************************************************/
-bool patcher_invokeinterface(u1 *sp)
+bool patcher_invokeinterface(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
+ uint8_t *ra;
unresolved_method *um;
methodinfo *m;
/* get stuff from the stack */
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- um = (unresolved_method *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
+ ra = (uint8_t *) pr->mpc;
+ um = (unresolved_method *) pr->ref;
/* get the fieldinfo */
- if (!(m = helper_resolve_methodinfo(um))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(m = resolve_method_eager(um)))
return false;
- }
-
- /* patch back original code */
- *((u8 *) ra) = mcode;
+ PATCH_BACK_ORIGINAL_MCODE;
/* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
+ if (opt_shownops)
+ ra = ra + PATCHER_CALL_SIZE;
/* patch interfacetable index */
- *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
- sizeof(methodptr) * m->class->index);
+ *((int32_t *) (ra + 3 + 3)) =
+ (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
+ sizeof(methodptr) * m->clazz->index);
/* patch method offset */
- *((s4 *) (ra + 3 + 7 + 3)) =
- (s4) (sizeof(methodptr) * (m - m->class->methods));
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
-
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *((int32_t *) (ra + 3 + 7 + 3)) =
+ (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
return true;
}
-/* patcher_checkcast_instanceof_flags ******************************************
-
- XXX
-
-*******************************************************************************/
-
-bool patcher_checkcast_instanceof_flags(u1 *sp)
-{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- constant_classref *cr;
- classinfo *c;
-
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* get the fieldinfo */
-
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
- return false;
- }
-
- /* patch back original code */
-
- *((u8 *) ra) = mcode;
-
- /* if we show disassembly, we have to skip the nop's */
-
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch class flags */
-
- *((s4 *) (ra + 2)) = (s4) c->flags;
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
-
- o->vftbl = (vftbl_t *) 1;
-#endif
-
- return true;
-}
-
+/* patcher_checkcast_interface *************************************************
-/* patcher_checkcast_instanceof_interface **************************************
+ Machine code:
- XXX
+ <patched call position>
+ 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
+ 41 81 fb 00 00 00 00 cmp $0x0,%r11d
+ 0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
+ 48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
+ 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
*******************************************************************************/
-bool patcher_checkcast_instanceof_interface(u1 *sp)
+bool patcher_checkcast_interface(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
+ uint8_t *ra;
constant_classref *cr;
classinfo *c;
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
+ ra = (uint8_t *) pr->mpc;
+ cr = (constant_classref *) pr->ref;
/* get the fieldinfo */
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(c = resolve_classref_eager(cr)))
return false;
- }
-
- /* patch back original code */
- *((u8 *) ra) = mcode;
+ PATCH_BACK_ORIGINAL_MCODE;
/* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
+ if (opt_shownops)
+ ra = ra + PATCHER_CALL_SIZE;
/* patch super class index */
- *((s4 *) (ra + 7 + 3)) = (s4) c->index;
+ *((int32_t *) (ra + 7 + 3)) = c->index;
- *((s4 *) (ra + 7 + 7 + 3 + 6 + 3)) =
- (s4) (OFFSET(vftbl_t, interfacetable[0]) -
- c->index * sizeof(methodptr*));
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
-
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *((int32_t *) (ra + 7 + 7 + 6 + 8 + 3)) =
+ (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
+ c->index * sizeof(methodptr*));
return true;
}
-/* patcher_checkcast_class *****************************************************
-
- XXX
-
-*******************************************************************************/
-
-bool patcher_checkcast_class(u1 *sp)
-{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- constant_classref *cr;
- classinfo *c;
-
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* get the fieldinfo */
-
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
- return false;
- }
+/* patcher_instanceof_interface ************************************************
- /* patch back original code */
-
- *((u8 *) ra) = mcode;
-
- /* if we show disassembly, we have to skip the nop's */
-
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch super class' vftbl */
-
- *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
- *((ptrint *) (ra + 10 + 7 + 7 + 3 + 2)) = (ptrint) c->vftbl;
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
-
- o->vftbl = (vftbl_t *) 1;
-#endif
-
- return true;
-}
-
-
-/* patcher_instanceof_class ****************************************************
+ Machine code:
- XXX
+ <patched call position>
+ 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
+ 41 81 fb 00 00 00 00 cmp $0x0,%r11d
+ 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
+ 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
*******************************************************************************/
-bool patcher_instanceof_class(u1 *sp)
+bool patcher_instanceof_interface(patchref_t *pr)
{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
+ uint8_t *ra;
constant_classref *cr;
classinfo *c;
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- cr = (constant_classref *) *((ptrint *) (sp + 0 * 8));
-
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
- *dontfillinexceptionstacktrace=true;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
+ ra = (uint8_t *) pr->mpc;
+ cr = (constant_classref *) pr->ref;
/* get the fieldinfo */
- if (!(c = helper_resolve_classinfo(cr))) {
- *dontfillinexceptionstacktrace=false;
+ if (!(c = resolve_classref_eager(cr)))
return false;
- }
- /* patch back original code */
-
- *((u8 *) ra) = mcode;
+ PATCH_BACK_ORIGINAL_MCODE;
/* if we show disassembly, we have to skip the nop's */
- if (showdisassemble)
- ra = ra + 5;
-
- /* patch super class' vftbl */
-
- *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
-
- *dontfillinexceptionstacktrace=false;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
-
- o->vftbl = (vftbl_t *) 1;
-#endif
-
- return true;
-}
+ if (opt_shownops)
+ ra = ra + PATCHER_CALL_SIZE;
+ /* patch super class index */
-/* patcher_clinit **************************************************************
-
- XXX
-
-*******************************************************************************/
-
-bool patcher_clinit(u1 *sp)
-{
- u1 *ra;
- java_objectheader *o;
- u8 mcode;
- classinfo *c;
- void *beginJavaStack;
-
- /* get stuff from the stack */
-
- ra = (u1 *) *((ptrint *) (sp + 3 * 8));
- o = (java_objectheader *) *((ptrint *) (sp + 2 * 8));
- mcode = *((u8 *) (sp + 1 * 8));
- c = (classinfo *) *((ptrint *) (sp + 0 * 8));
-
- beginJavaStack = (void*) (sp + 3 * 8);
-
- /*printf("beginJavaStack: %p, ra %p\n",beginJavaStack,ra);*/
- /* calculate and set the new return address */
-
- ra = ra - 5;
- *((ptrint *) (sp + 3 * 8)) = (ptrint) ra;
-
-#if defined(USE_THREADS)
- /* enter a monitor on the patching position */
-
- builtin_monitorenter(o);
-
- /* check if the position has already been patched */
-
- if (o->vftbl) {
- builtin_monitorexit(o);
-
- return true;
- }
-#endif
-
- /* check if the class is initialized */
-
- if (!c->initialized) {
- bool init;
- {
- /*struct native_stackframeinfo {
- void *oldThreadspecificHeadValue;
- void **addressOfThreadspecificHead;
- methodinfo *method;
- void *beginOfJavaStackframe; only used if != 0
- void *returnToFromNative;
- }*/
- /* more or less the same as the above sfi setup is done in the assembler code by the prepare/remove functions*/
- native_stackframeinfo sfi;
- sfi.returnToFromNative=(void*)ra;
- sfi.beginOfJavaStackframe=beginJavaStack;
- sfi.method=0; /*internal*/
- sfi.addressOfThreadspecificHead=builtin_asm_get_stackframeinfo();
- sfi.oldThreadspecificHeadValue=*(sfi.addressOfThreadspecificHead);
- *(sfi.addressOfThreadspecificHead)=&sfi;
-
- init=initialize_class(c);
-
- *(sfi.addressOfThreadspecificHead)=sfi.oldThreadspecificHeadValue;
- }
- if (!init)
- {
- return false;
- }
- }
-
- /* patch back original code */
-
- *((u8 *) ra) = mcode;
-
-#if defined(USE_THREADS)
- /* leave the monitor on the patching position */
-
- builtin_monitorexit(o);
-
- /* this position has been patched */
+ *((int32_t *) (ra + 7 + 3)) = c->index;
- o->vftbl = (vftbl_t *) 1;
-#endif
+ *((int32_t *) (ra + 7 + 7 + 6 + 3)) =
+ (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
+ c->index * sizeof(methodptr*));
return true;
}