X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Fvm%2Fjit%2Fx86_64%2Fpatcher.c;h=81cc09e0e8bbc78583471327af0d14c9203634c0;hb=67da78971605bb8a2953a9f6d5a7862857049538;hp=b5356be9860c52b74f5acd38a544e3e39ef68345;hpb=9300fac58530f97fec3ae41a9b4f116dce2809ff;p=cacao.git diff --git a/src/vm/jit/x86_64/patcher.c b/src/vm/jit/x86_64/patcher.c index b5356be98..81cc09e0e 100644 --- a/src/vm/jit/x86_64/patcher.c +++ b/src/vm/jit/x86_64/patcher.c @@ -1,9 +1,7 @@ /* 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, 2009 + CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO This file is part of CACAO. @@ -19,1402 +17,509 @@ 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. + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. - Contact: cacao@complang.tuwien.ac.at +*/ - Authors: Christian Thalinger - Changes: +#include "config.h" - $Id: patcher.c 2426 2005-04-30 20:12:59Z twisti $ +#include -*/ +#include "vm/types.h" + +#include "vm/jit/x86_64/codegen.h" +#include "vm/jit/x86_64/md.h" + +#include "mm/memory.hpp" +#include "native/native.hpp" -#include "vm/jit/x86_64/types.h" -#include "vm/builtin.h" -#include "vm/field.h" -#include "vm/initialize.h" +#include "vm/jit/builtin.hpp" +#include "vm/class.hpp" +#include "vm/field.hpp" +#include "vm/initialize.hpp" #include "vm/options.h" #include "vm/references.h" -#include "vm/jit/helper.h" -#include "vm/exceptions.h" - - -helper_initialize_class(void* beginOfJavaStack,classinfo *c,u1 *ra) { - 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=ra; - sfi.beginOfJavaStackframe=beginOfJavaStack; - sfi.method=0; /*internal*/ - sfi.addressOfThreadspecificHead=builtin_asm_get_stackframeinfo(); - sfi.oldThreadspecificHeadValue=*(sfi.addressOfThreadspecificHead); - *(sfi.addressOfThreadspecificHead)=&sfi; - - /*printf("calling static initializer (helper_initialize_class), returnaddress=%p for class %s\n",ra,c->name->text);*/ - - init=initialize_class(c); - - *(sfi.addressOfThreadspecificHead)=sfi.oldThreadspecificHeadValue; - } - return init; - } - return true; -} +#include "vm/resolve.hpp" +#include "vm/jit/patcher-common.hpp" -/* patcher_get_putstatic ******************************************************* +/* patcher_patch_code ********************************************************** - Machine code: - - - 4d 8b 15 86 fe ff ff mov -378(%rip),%r10 + Just patches back the original machine code. *******************************************************************************/ -bool patcher_get_putstatic(u1 *sp) +void patcher_patch_code(patchref_t *pr) { - 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 */ - - if (o->vftbl) { - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(fi = helper_resolve_fieldinfo(uf))) { - *dontfillinexceptionstacktrace=false; - return false; - } - /* check if the field's class is initialized */ - - *dontfillinexceptionstacktrace=false; - if (!helper_initialize_class(beginJavaStack,fi->class,ra)) 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; - - /* get RIP offset from machine instruction */ - - offset = *((u4 *) (ra + 3)); - - /* patch the field value's address (+ 7: is the size of the RIP move) */ - - *((ptrint *) (ra + 7 + offset)) = (ptrint) &(fi->value); - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - *dontfillinexceptionstacktrace=true; - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - -#endif - - return true; + *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode; + md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE); } - -/* patcher_get_putfield ******************************************************** - - Machine code: - - - 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d - -*******************************************************************************/ - -bool patcher_get_putfield(u1 *sp) +/** + * Check if the trap instruction at the given PC is valid. + * + * @param pc Program counter. + * + * @return true if valid, false otherwise. + */ +bool patcher_is_valid_trap_instruction_at(void* pc) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_field *uf; - fieldinfo *fi; - - /* get stuff from the stack */ + uint16_t mcode = *((uint16_t*) pc); - 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 */ - - if (o->vftbl) { - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(fi = helper_resolve_fieldinfo(uf))) { - *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 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); - } - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; + // Check for the undefined instruction we use. + return (mcode == 0x0b0f); +} - /* leave the monitor on the patching position */ - builtin_monitorexit(o); +/* patcher_resolve_classref_to_classinfo *************************************** -#endif - *dontfillinexceptionstacktrace=false; + ACONST: - return true; -} + + 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi + MULTIANEWARRAY: -/* patcher_putfieldconst ******************************************************* + + 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: - 49 c7 87 10 00 00 00 00 00 00 00 movq $0x0,0x10(%r15) + 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_putfieldconst(u1 *sp) +bool patcher_resolve_classref_to_classinfo(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_field *uf; - fieldinfo *fi; + constant_classref* cr = (constant_classref*) pr->ref; + uintptr_t* datap = (uintptr_t*) pr->datap; - /* get stuff from the stack */ + // Resolve the class. + classinfo* c = resolve_classref_eager(cr); - 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 */ - - if (o->vftbl) { - builtin_monitorexit(o); - - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(fi = helper_resolve_fieldinfo(uf))) { - *dontfillinexceptionstacktrace=false; + if (c == NULL) return false; - } - - /* patch back original code */ - *((u8 *) ra) = mcode; + // Patch the class. + *datap = (uintptr_t) c; - /* if we show disassembly, we have to skip the nop's */ + // Synchronize data cache. + md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P); - if (showdisassemble) - ra = ra + 5; - - /* handle special case when the base register is %r12 */ - - if (*(ra + 2) == 0x84) - ra = ra + 1; - - /* patch the field's offset */ - - if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) { - *((u4 *) (ra + 3)) = (u4) (fi->offset); - *((u4 *) (ra + 11 + 3)) = (u4) (fi->offset + 4); - - } else { - *((u4 *) (ra + 3)) = (u4) (fi->offset); - } - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; + // Patch back the original code. + patcher_patch_code(pr); 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 - 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; - 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)); - cr = (constant_classref *) *((ptrint *) (sp + 0 * 8)); - beginJavaStack = (void*) (sp+3*8); - - /* calculate and set the new return address */ - - ra = ra - (10 + 5); - *((ptrint *) (sp + 3 * 8)) = (ptrint) ra; - - *dontfillinexceptionstacktrace=true; - -#if defined(USE_THREADS) - /* enter a monitor on the patching position */ + constant_classref* cr = (constant_classref*) pr->ref; + uintptr_t* datap = (uintptr_t*) pr->datap; - builtin_monitorenter(o); + // Resolve the field. + classinfo* c = resolve_classref_eager(cr); - /* check if the position has already been patched */ - - if (o->vftbl) { - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the classinfo */ - - if (!(c = helper_resolve_classinfo(cr))) { - *dontfillinexceptionstacktrace=false; - /*should here be an monitorexit too ?*/ + if (c == NULL) return false; - } - - *dontfillinexceptionstacktrace=false; - /*printf("ra:%p\n",ra);*/ - if (!helper_initialize_class(beginJavaStack,c,ra+5)) return false; - - /* patch back original code */ - *((u8 *) (ra + 10)) = mcode; + // Patch super class' vftbl. + *datap = (uintptr_t) c->vftbl; - /* patch the classinfo pointer */ + // Synchronize data cache. + md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P); - *((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; - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - *dontfillinexceptionstacktrace=true; - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - -#endif + // Patch back the original code. + patcher_patch_code(pr); 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 - 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; - 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)); + constant_classref* cr = (constant_classref*) pr->ref; +/* int32_t* datap = (int32_t*) pr->datap; */ + uint8_t* ra = (uint8_t*) pr->mpc; - /* calculate and set the new return address */ + // Resolve the field. + classinfo* c = resolve_classref_eager(cr); - ra = ra - (10 + 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); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the classinfo */ - - if (!(c = helper_resolve_classinfo(cr))) { - *dontfillinexceptionstacktrace=false; + if (c == NULL) return false; - } - - /* patch back original code */ - - *((u8 *) (ra + 10)) = mcode; - - /* patch the class' vftbl pointer */ - - *((ptrint *) (ra + 2)) = (ptrint) c->vftbl; - /* if we show disassembly, we have to skip the nop's */ + ra += PATCHER_CALL_SIZE; - if (showdisassemble) - ra = ra + 5; + // Patch class flags. +/* *datap = c->flags; */ + *((int32_t*) (ra + 2)) = c->flags; - /* patch new function address */ + // Synchronize data cache. +/* md_dcacheflush(datap, sizeof(int32_t)); */ + md_icacheflush(ra + 2, sizeof(int32_t)); - *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_newarray; - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); - -#endif - *dontfillinexceptionstacktrace=false; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_builtin_multianewarray ********************************************** +/* patcher_get_putstatic ******************************************************* Machine code: - 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 */ - - 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)); + unresolved_field* uf = (unresolved_field*) pr->ref; + uintptr_t* datap = (uintptr_t*) pr->datap; + uint8_t* ra = (uint8_t*) pr->mpc; - /* calculate and set the new return address */ + // Resolve the field. + fieldinfo* fi = resolve_field_eager(uf); - 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); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the classinfo */ - - if (!(c = helper_resolve_classinfo(cr))) { - *dontfillinexceptionstacktrace=false; + if (fi == NULL) 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; + ra += PATCHER_CALL_SIZE; - /* patch new function address */ + // Check if the field's class is initialized/ + if (!(fi->clazz->state & CLASS_INITIALIZED)) + if (!initialize_class(fi->clazz)) + return false; - *((ptrint *) (ra + 10 + 10 + 3 + 2)) = (ptrint) BUILTIN_multianewarray; + // Patch the field value's address. + *datap = (uintptr_t) fi->value; + // Synchronize data cache. + md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P); -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_builtin_checkarraycast ********************************************** +/* patcher_get_putfield ******************************************************** Machine code: - 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 + 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 */ + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_field* uf = (unresolved_field*) pr->ref; - ra = ra - (10 + 5); - *((ptrint *) (sp + 3 * 8)) = (ptrint) ra; + // Resolve the field. + fieldinfo* fi = resolve_field_eager(uf); - *dontfillinexceptionstacktrace=true; - -#if defined(USE_THREADS) - /* enter a monitor on the patching position */ - - builtin_monitorenter(o); + if (fi == NULL) + return false; - /* check if the position has already been patched */ + pc += PATCHER_CALL_SIZE; - if (o->vftbl) { - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - return true; + // Patch the field's offset: we check for the field type, because + // the instructions have different lengths. + if (IS_INT_LNG_TYPE(fi->type)) { + // Check for special case: %rsp or %r12 as base register. + if (pc[3] == 0x24) + *((int32_t*) (pc + 4)) = fi->offset; + else + *((int32_t*) (pc + 3)) = fi->offset; } -#endif - - /* get the classinfo */ - - if (!(c = helper_resolve_classinfo(cr))) { - *dontfillinexceptionstacktrace=false; - return false; + else { + // Check for special case: %rsp or %r12 as base register. + if (pc[5] == 0x24) + *((int32_t*) (pc + 6)) = fi->offset; + else + *((int32_t*) (pc + 5)) = fi->offset; } - /* patch back original code */ - - *((u8 *) (ra + 10)) = mcode; - - /* patch the class' vftbl pointer */ - - *((ptrint *) (ra + 2)) = (ptrint) c->vftbl; - - /* 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_checkarraycast; - + // Synchronize instruction cache. + md_icacheflush(pc, 6 + sizeof(int32_t)); -#if defined(USE_THREADS) - /* this position has been patched */ + // Patch back the original code. + patcher_patch_code(pr); - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; return true; } -/* patcher_builtin_arrayinstanceof ********************************************* +/* patcher_putfieldconst ******************************************************* Machine code: - 48 be 30 3c b2 00 00 00 00 00 mov $0xb23c30,%rsi - 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)); + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_field* uf = (unresolved_field*) pr->ref; - /* calculate and set the new return address */ + // Resolve the field. + fieldinfo* fi = resolve_field_eager(uf); - ra = ra - (10 + 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 (fi == NULL) + return false; - if (o->vftbl) { - builtin_monitorexit(o); + pc += PATCHER_CALL_SIZE; - return true; + // Patch the field's offset. + if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) { + // Handle special case when the base register is %r12. + if (pc[12] == 0x94) + *((uint32_t*) (pc + 14)) = fi->offset; + else + *((uint32_t*) (pc + 13)) = fi->offset; } -#endif - - /* get the classinfo */ - - if (!(c = helper_resolve_classinfo(cr))) { - *dontfillinexceptionstacktrace=false; - return false; + else { + // Handle special case when the base register is %r12. + if (pc[2] == 0x84) + *((uint32_t*) (pc + 4)) = fi->offset; + else + *((uint32_t*) (pc + 3)) = fi->offset; } - /* patch back original code */ - - *((u8 *) (ra + 10)) = mcode; - - /* patch the class' vftbl pointer */ - - *((ptrint *) (ra + 2)) = (ptrint) c->vftbl; - - /* 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_arrayinstanceof; - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; + // Synchronize instruction cache. + md_icacheflush(pc, 14 + sizeof(int32_t)); - /* leave the monitor on the patching position */ + // Patch back the original code. + patcher_patch_code(pr); - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; return true; } /* patcher_invokestatic_special ************************************************ - XXX + Machine code: + + + 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; - 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; + unresolved_method* um = (unresolved_method*) pr->ref; + uintptr_t* datap = (uintptr_t*) pr->datap; - *dontfillinexceptionstacktrace=true; + // Resolve the method. + methodinfo* m = resolve_method_eager(um); -#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); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(m = helper_resolve_methodinfo(um))) { - *dontfillinexceptionstacktrace=false; + if (m == NULL) return false; - } - /* patch back original code */ - *((u8 *) ra) = mcode; + // Patch stubroutine. + *datap = (uintptr_t) m->stubroutine; - /* if we show disassembly, we have to skip the nop's */ + // Synchronize data cache. + md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P); - if (showdisassemble) - ra = ra + 5; + // Patch back the original code. + patcher_patch_code(pr); - /* patch stubroutine */ - - *((ptrint *) (ra + 2)) = (ptrint) m->stubroutine; - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; return true; } /* patcher_invokevirtual ******************************************************* - XXX + Machine code: + + + 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; - 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; + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_method* um = (unresolved_method*) pr->ref; - *dontfillinexceptionstacktrace=true; + // Resovlve the method. + methodinfo* m = resolve_method_eager(um); -#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); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(m = helper_resolve_methodinfo(um))) { - *dontfillinexceptionstacktrace=false; + if (m == NULL) return false; - } - /* patch back original code */ + pc += PATCHER_CALL_SIZE; - *((u8 *) ra) = mcode; + // Patch vftbl index. + *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex); - /* if we show disassembly, we have to skip the nop's */ + // Synchronize instruction cache. + md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P); - if (showdisassemble) - ra = ra + 5; + // Patch back the original code. + patcher_patch_code(pr); - /* patch vftbl index */ - - *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, table[0]) + - sizeof(methodptr) * m->vftblindex); - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; return true; } /* patcher_invokeinterface ***************************************************** - XXX - -*******************************************************************************/ - -bool patcher_invokeinterface(u1 *sp) -{ - u1 *ra; - java_objectheader *o; - u8 mcode; - 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); - *dontfillinexceptionstacktrace=true; - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(m = helper_resolve_methodinfo(um))) { - *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 interfacetable index */ - - *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, interfacetable[0]) - - sizeof(methodptr) * m->class->index); - - /* patch method offset */ - - *((s4 *) (ra + 3 + 7 + 3)) = - (s4) (sizeof(methodptr) * (m - m->class->methods)); - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; - return true; -} - - -/* patcher_checkcast_instanceof_flags ****************************************** + Machine code: - XXX + + 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_checkcast_instanceof_flags(u1 *sp) +bool patcher_invokeinterface(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 - 5; - *((ptrint *) (sp + 3 * 8)) = (ptrint) ra; + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_method* um = (unresolved_method*) pr->ref; - *dontfillinexceptionstacktrace=true; + // Resolve the method. + methodinfo* m = resolve_method_eager(um); -#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); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(c = helper_resolve_classinfo(cr))) { - *dontfillinexceptionstacktrace=false; + if (m == NULL) return false; - } - /* patch back original code */ + pc += PATCHER_CALL_SIZE; - *((u8 *) ra) = mcode; + // Patch interfacetable index. + *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index); - /* if we show disassembly, we have to skip the nop's */ + // Patch method offset. + *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods)); - if (showdisassemble) - ra = ra + 5; + // Synchronize instruction cache. + md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P); - /* patch class flags */ + // Patch back the original code. + patcher_patch_code(pr); - *((s4 *) (ra + 2)) = (s4) c->flags; - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; return true; } -/* patcher_checkcast_instanceof_interface ************************************** - - XXX - -*******************************************************************************/ - -bool patcher_checkcast_instanceof_interface(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); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the fieldinfo */ +/* patcher_checkcast_interface ************************************************* - 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 super class index */ - - *((s4 *) (ra + 7 + 3)) = (s4) c->index; - - *((s4 *) (ra + 7 + 7 + 3 + 6 + 3)) = - (s4) (OFFSET(vftbl_t, interfacetable[0]) - - c->index * sizeof(methodptr*)); - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; - return true; -} - - -/* patcher_checkcast_class ***************************************************** + Machine code: - XXX + + 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_class(u1 *sp) +bool patcher_checkcast_interface(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 - 5; - *((ptrint *) (sp + 3 * 8)) = (ptrint) ra; - - *dontfillinexceptionstacktrace=true; - -#if defined(USE_THREADS) - /* enter a monitor on the patching position */ - - builtin_monitorenter(o); + uint8_t* pc = (uint8_t*) pr->mpc; + constant_classref* cr = (constant_classref*) pr->ref; - /* check if the position has already been patched */ + // Resolve the class. + classinfo* c = resolve_classref_eager(cr); - if (o->vftbl) { - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* get the fieldinfo */ - - if (!(c = helper_resolve_classinfo(cr))) { - *dontfillinexceptionstacktrace=false; + if (c == NULL) 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; + pc += PATCHER_CALL_SIZE; - /* patch super class' vftbl */ + // Patch super class index. + *((int32_t*) (pc + 7 + 3)) = c->index; - *((ptrint *) (ra + 2)) = (ptrint) c->vftbl; - *((ptrint *) (ra + 10 + 7 + 7 + 3 + 2)) = (ptrint) c->vftbl; + *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*)); + // Synchronize instruction cache. + md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t)); -#if defined(USE_THREADS) - /* this position has been patched */ + // Patch back the original code. + patcher_patch_code(pr); - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; return true; } -/* patcher_instanceof_class **************************************************** - - XXX - -*******************************************************************************/ - -bool patcher_instanceof_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); - *dontfillinexceptionstacktrace=false; - 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; - - -#if defined(USE_THREADS) - /* this position has been patched */ - - o->vftbl = (vftbl_t *) 1; - - /* leave the monitor on the patching position */ - - builtin_monitorexit(o); -#endif - *dontfillinexceptionstacktrace=false; - return true; -} - - -/* patcher_clinit ************************************************************** + Machine code: - XXX + + 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_clinit(u1 *sp) +bool patcher_instanceof_interface(patchref_t *pr) { - 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; + uint8_t* pc = (uint8_t*) pr->mpc; + constant_classref* cr = (constant_classref*) pr->ref; -#if defined(USE_THREADS) - /* enter a monitor on the patching position */ + // Resolve the class. + classinfo* c = resolve_classref_eager(cr); - builtin_monitorenter(o); - - /* check if the position has already been patched */ - - if (o->vftbl) { - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; - return true; - } -#endif - - /* check if the class is initialized */ - *dontfillinexceptionstacktrace=false; - if (!helper_initialize_class(beginJavaStack,c,ra)) return false; - - /* patch back original code */ + if (c == NULL) + return false; - *((u8 *) ra) = mcode; + pc += PATCHER_CALL_SIZE; -#if defined(USE_THREADS) - /* this position has been patched */ + // Patch super class index. + *((int32_t*) (pc + 7 + 3)) = c->index; - o->vftbl = (vftbl_t *) 1; + *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*)); - /* leave the monitor on the patching position */ - *dontfillinexceptionstacktrace=true; - builtin_monitorexit(o); - *dontfillinexceptionstacktrace=false; + // Synchronize instruction cache. + md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t)); -#endif + // Patch back the original code. + patcher_patch_code(pr); return true; }