X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Fvm%2Fjit%2Fx86_64%2Fpatcher.c;h=b7fcccb633ab59b995b90525b61df249ce37ee48;hb=c414f560a7e0b7636a780e6b933e91e441f80503;hp=b5b47d71e5416bf291137052c9c73795d6e202c2;hpb=ad92477479aeed17382996ab43a7ca0dfab2ba93;p=cacao.git diff --git a/src/vm/jit/x86_64/patcher.c b/src/vm/jit/x86_64/patcher.c index b5b47d71e..b7fcccb63 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, 2006 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-2011 + CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO This file is part of CACAO. @@ -22,392 +20,111 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - Contact: cacao@cacaojvm.org +*/ - Authors: Christian Thalinger - Changes: +#include "config.h" - $Id: patcher.c 4357 2006-01-22 23:33:38Z twisti $ +#include -*/ +#include "vm/types.h" +#include "vm/jit/x86_64/codegen.h" +#include "vm/jit/x86_64/md.h" -#include "config.h" -#include "vm/types.h" +#include "mm/memory.hpp" + +#include "native/native.hpp" -#include "mm/memory.h" -#include "native/native.h" -#include "vm/builtin.h" -#include "vm/class.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/resolve.h" -#include "vm/jit/patcher.h" +#include "vm/resolve.hpp" +#include "vm/jit/patcher-common.hpp" -/* patcher_get_putstatic ******************************************************* - Machine code: +/* patcher_patch_code ********************************************************** - - 4d 8b 15 86 fe ff ff mov -378(%rip),%r10 - 49 8b 32 mov (%r10),%rsi + 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; - s4 disp; - fieldinfo *fi; - s4 offset; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - uf = (unresolved_field *) *((ptrint *) (sp + 1 * 8)); - disp = *((s4 *) (sp + 0 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the fieldinfo */ - - if (!(fi = resolve_field_eager(uf))) { - PATCHER_MONITOREXIT; - - return false; - } - - /* check if the field's class is initialized */ - - if (!(fi->class->state & CLASS_INITIALIZED)) { - if (!initialize_class(fi->class)) { - PATCHER_MONITOREXIT; - - return false; - } - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - - /* if we show disassembly, we have to skip the nop's */ - - if (opt_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); - - PATCHER_MARK_PATCHED_MONITOREXIT; - - 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) +static int32_t *patch_checked_location(int32_t *p, int32_t val) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_field *uf; - fieldinfo *fi; - u1 byte; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - uf = (unresolved_field *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the fieldinfo */ - - if (!(fi = resolve_field_eager(uf))) { - PATCHER_MONITOREXIT; - - return false; - } - - /* patch back original code (instruction code is smaller than 8 bytes) */ - - *((u4 *) (ra + 0)) = (u4) mcode; - *((u1 *) (ra + 4)) = (u1) (mcode >> 32); - - /* if we show disassembly, we have to skip the nop's */ - - if (opt_showdisassemble) - ra = ra + 5; - - /* 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 */ - - byte = *(ra + 3); - - if (byte == 0x24) - *((u4 *) (ra + 4)) = (u4) (fi->offset); - else - *((u4 *) (ra + 3)) = (u4) (fi->offset); - - } else { - /* check for special case: %rsp or %r12 as base register */ - - byte = *(ra + 5); - - if (byte == 0x24) - *((u4 *) (ra + 6)) = (u4) (fi->offset); - else - *((u4 *) (ra + 5)) = (u4) (fi->offset); - } - - PATCHER_MARK_PATCHED_MONITOREXIT; - - return true; + assert(*p == 0); + // verify that it's aligned + assert((((uintptr_t) p) & (4-1)) == 0); + *p = val; + return p; } - -/* patcher_putfieldconst ******************************************************* - - Machine code: - - - 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13) - -*******************************************************************************/ - -bool patcher_putfieldconst(u1 *sp) +static void checked_icache_flush(int32_t *addr, int nbytes, int32_t *check_loc) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_field *uf; - fieldinfo *fi; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - uf = (unresolved_field *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the fieldinfo */ - - if (!(fi = resolve_field_eager(uf))) { - PATCHER_MONITOREXIT; - - return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - - /* if we show disassembly, we have to skip the nop's */ - - if (opt_showdisassemble) - ra = ra + 5; - - /* 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 (*(ra + 2) == 0x84) { - *((u4 *) (ra + 4)) = (u4) (fi->offset); - *((u4 *) (ra + 12 + 4)) = (u4) (fi->offset + 4); + assert((int8_t*) addr + nbytes - sizeof(int32_t) >= (int8_t*) check_loc); + md_icacheflush(addr, nbytes); +} - } else { - *((u4 *) (ra + 3)) = (u4) (fi->offset); - *((u4 *) (ra + 11 + 3)) = (u4) (fi->offset + 4); - } +/** + * 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) +{ + uint16_t mcode = *((uint16_t*) pc); - } else { - /* handle special case when the base register is %r12 */ + // Check for the undefined instruction we use. + return (mcode == 0x0b0f); +} - if (*(ra + 2) == 0x84) - *((u4 *) (ra + 4)) = (u4) (fi->offset); - else - *((u4 *) (ra + 3)) = (u4) (fi->offset); - } +/** + * Overwrites the MFENCE instruction at the indicated address with a 3-byte + * NOP. The MFENCE instruction is not allowed to cross a (4-byte) word + * boundary. + * + * @param pc Program counter. + */ +static void patch_out_mfence(void *pc) +{ + uint32_t *p = (uint32_t*) (((uintptr_t) pc) & ~3); - PATCHER_MARK_PATCHED_MONITOREXIT; + assert((((uintptr_t) pc) & 3) < 2); + if (((uintptr_t) pc) & 1) + *p = (*p & 0x000000ff) | 0x001f0f00; + else + *p = (*p & 0xff000000) | 0x00001f0f; - return true; + md_icacheflush(p, 4); } +/* patcher_resolve_classref_to_classinfo *************************************** -/* patcher_aconst ************************************************************** - - Machine code: + ACONST: 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi -*******************************************************************************/ - -bool patcher_aconst(u1 *sp) -{ - u1 *ra; - java_objectheader *o; - u8 mcode; - constant_classref *cr; - classinfo *c; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - cr = (constant_classref *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the classinfo */ - - if (!(c = resolve_classref_eager(cr))) { - PATCHER_MONITOREXIT; - - return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - - /* if we show disassembly, we have to skip the nop's */ - - if (opt_showdisassemble) - ra = ra + 5; - - /* patch the classinfo pointer */ - - *((ptrint *) (ra + 2)) = (ptrint) c; - - PATCHER_MARK_PATCHED_MONITOREXIT; - - return true; -} - - -/* patcher_builtin_multianewarray ********************************************** - - Machine code: + MULTIANEWARRAY: - 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 -*******************************************************************************/ - -bool patcher_builtin_multianewarray(u1 *sp) -{ - u1 *ra; - java_objectheader *o; - u8 mcode; - constant_classref *cr; - classinfo *c; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - cr = (constant_classref *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the classinfo */ - - if (!(c = resolve_classref_eager(cr))) { - PATCHER_MONITOREXIT; - - return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - - /* if we show disassembly, we have to skip the nop's */ - - if (opt_showdisassemble) - ra = ra + 5; - - /* patch the classinfo pointer */ - - *((ptrint *) (ra + 10 + 2)) = (ptrint) c; - - /* patch new function address */ - - *((ptrint *) (ra + 10 + 10 + 3 + 2)) = (ptrint) BUILTIN_multianewarray; - - PATCHER_MARK_PATCHED_MONITOREXIT; - - return true; -} - - -/* patcher_builtin_arraycheckcast ********************************************** - - Machine code: + ARRAYCHECKCAST: 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi @@ -416,650 +133,423 @@ bool patcher_builtin_multianewarray(u1 *sp) *******************************************************************************/ -bool patcher_builtin_arraycheckcast(u1 *sp) +bool patcher_resolve_classref_to_classinfo(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - constant_classref *cr; - classinfo *c; - - /* get stuff from the stack */ + constant_classref* cr = (constant_classref*) pr->ref; + uintptr_t* datap = (uintptr_t*) pr->datap; - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - cr = (constant_classref *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the classinfo */ - - if (!(c = resolve_classref_eager(cr))) { - PATCHER_MONITOREXIT; + // Resolve the class. + classinfo* c = resolve_classref_eager(cr); + if (c == NULL) return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - - /* if we show disassembly, we have to skip the nop's */ - - if (opt_showdisassemble) - ra = ra + 5; - /* patch the classinfo pointer */ + // Patch the class. + *datap = (uintptr_t) c; - *((ptrint *) (ra + 2)) = (ptrint) c; + // Synchronize data cache. + md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P); - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_invokestatic_special ************************************************ +/* patcher_resolve_classref_to_vftbl ******************************************* - Machine code: + CHECKCAST (class): + INSTANCEOF (class): - 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_resolve_classref_to_vftbl(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_method *um; - methodinfo *m; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - um = (unresolved_method *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; + constant_classref* cr = (constant_classref*) pr->ref; + uintptr_t* datap = (uintptr_t*) pr->datap; - /* get the fieldinfo */ - - if (!(m = resolve_method_eager(um))) { - PATCHER_MONITOREXIT; + // Resolve the field. + classinfo* c = resolve_classref_eager(cr); + if (c == NULL) return false; - } - /* patch back original code */ - - *((u8 *) ra) = mcode; - - /* if we show disassembly, we have to skip the nop's */ - if (opt_showdisassemble) - ra = ra + 5; + // Patch super class' vftbl. + *datap = (uintptr_t) c->vftbl; - /* patch stubroutine */ + // Synchronize data cache. + md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P); - *((ptrint *) (ra + 2)) = (ptrint) m->stubroutine; - - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_invokevirtual ******************************************************* +/* patcher_resolve_classref_to_flags ******************************************* - Machine code: + CHECKCAST/INSTANCEOF: - 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_resolve_classref_to_flags(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_method *um; - methodinfo *m; + constant_classref* cr = (constant_classref*) pr->ref; +/* int32_t* datap = (int32_t*) pr->datap; */ + uint8_t* ra = (uint8_t*) pr->mpc; - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - um = (unresolved_method *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the fieldinfo */ - - if (!(m = resolve_method_eager(um))) { - PATCHER_MONITOREXIT; + // Resolve the field. + classinfo* c = resolve_classref_eager(cr); + if (c == NULL) return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - /* if we show disassembly, we have to skip the nop's */ + ra += PATCHER_CALL_SIZE; + ra += PATCH_ALIGNMENT((uintptr_t) ra, 2, sizeof(int32_t)); - if (opt_showdisassemble) - ra = ra + 5; + // Patch class flags. +/* *datap = c->flags; */ + patch_checked_location((int32_t*) (ra + 2), c->flags); - /* patch vftbl index */ + // Synchronize data cache. +/* md_dcacheflush(datap, sizeof(int32_t)); */ + md_icacheflush(ra + 2, sizeof(int32_t)); - *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, table[0]) + - sizeof(methodptr) * m->vftblindex); - - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_invokeinterface ***************************************************** +/* patcher_get_putstatic ******************************************************* Machine code: - 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 + 4d 8b 15 86 fe ff ff mov -378(%rip),%r10 + 49 8b 32 mov (%r10),%rsi *******************************************************************************/ -bool patcher_invokeinterface(u1 *sp) +bool patcher_get_putstatic(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_method *um; - methodinfo *m; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - um = (unresolved_method *) *((ptrint *) (sp + 1 * 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 */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the fieldinfo */ - - if (!(m = resolve_method_eager(um))) { - PATCHER_MONITOREXIT; + // Resolve the field. + fieldinfo* fi = resolve_field_eager(uf); + if (fi == NULL) return false; - } - - /* patch back original code */ - *((u8 *) ra) = mcode; + ra += PATCHER_CALL_SIZE; - /* if we show disassembly, we have to skip the nop's */ - - if (opt_showdisassemble) - ra = ra + 5; - - /* patch interfacetable index */ + // Check if the field's class is initialized/ + if (!(fi->clazz->state & CLASS_INITIALIZED)) + if (!initialize_class(fi->clazz)) + return false; - *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, interfacetable[0]) - - sizeof(methodptr) * m->class->index); + // Patch the field value's address. + *datap = (uintptr_t) fi->value; - /* patch method offset */ + if (pr->disp_mb && !(fi->flags & ACC_VOLATILE)) + patch_out_mfence(ra + pr->disp_mb - 2); - *((s4 *) (ra + 3 + 7 + 3)) = - (s4) (sizeof(methodptr) * (m - m->class->methods)); + // Synchronize data cache. + md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P); - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_checkcast_instanceof_flags ****************************************** +/* patcher_get_putfield ******************************************************** Machine code: - 41 ba 00 00 00 00 mov $0x0,%r10d - 41 81 e2 00 02 00 00 and $0x200,%r10d - 0f 84 35 00 00 00 je 0x00002aaaaab01479 + 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d *******************************************************************************/ -bool patcher_checkcast_instanceof_flags(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 + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - cr = (constant_classref *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_field* uf = (unresolved_field*) pr->ref; - PATCHER_MONITORENTER; - - /* get the fieldinfo */ - - if (!(c = resolve_classref_eager(cr))) { - PATCHER_MONITOREXIT; + // Resolve the field. + fieldinfo* fi = resolve_field_eager(uf); + if (fi == NULL) return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - /* if we show disassembly, we have to skip the nop's */ + pc += PATCHER_CALL_SIZE; - if (opt_showdisassemble) - ra = ra + 5; + int disp = -sizeof(int32_t) + pr->patch_align; + patch_checked_location((int32_t*) (pc + disp), fi->offset); - /* patch class flags */ + if (pr->disp_mb && !(fi->flags & ACC_VOLATILE)) + patch_out_mfence(pc + pr->disp_mb - 2); - *((s4 *) (ra + 2)) = (s4) c->flags; + // Synchronize instruction cache. + md_icacheflush(pc, disp + sizeof(int32_t)); - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_checkcast_instanceof_interface ************************************** +/* patcher_putfieldconst ******************************************************* Machine code: - 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d - 49 81 eb 00 00 00 00 sub $0x0,%r11 - 4d 85 db test %r11,%r11 - 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8 - 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11 + 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13) *******************************************************************************/ -bool patcher_checkcast_instanceof_interface(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 + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - cr = (constant_classref *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_field* uf = (unresolved_field*) pr->ref; - /* get the fieldinfo */ - - if (!(c = resolve_classref_eager(cr))) { - PATCHER_MONITOREXIT; + // Resolve the field. + fieldinfo* fi = resolve_field_eager(uf); + if (fi == NULL) return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - /* if we show disassembly, we have to skip the nop's */ + pc += PATCHER_CALL_SIZE; - if (opt_showdisassemble) - ra = ra + 5; + int disp = -2*sizeof(int32_t) + pr->patch_align; + patch_checked_location((int32_t*) (pc + disp), fi->offset); - /* patch super class index */ + if (pr->disp_mb && !(fi->flags & ACC_VOLATILE)) + patch_out_mfence(pc + pr->disp_mb - 2); - *((s4 *) (ra + 7 + 3)) = (s4) c->index; + // Synchronize instruction cache. + md_icacheflush(pc, disp + sizeof(int32_t)); - *((s4 *) (ra + 7 + 7 + 3 + 6 + 3)) = - (s4) (OFFSET(vftbl_t, interfacetable[0]) - - c->index * sizeof(methodptr*)); - - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_checkcast_class ***************************************************** +/* patcher_invokestatic_special ************************************************ Machine code: - 49 bb 00 00 00 00 00 00 00 00 mov $0x0,%r11 - 45 8b 92 20 00 00 00 mov 0x20(%r10),%r10d - 45 8b 9b 20 00 00 00 mov 0x20(%r11),%r11d - 4d 29 da sub %r11,%r10 - 49 bb 00 00 00 00 00 00 00 00 mov $0x0,%r11 + 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10 + 49 ff d2 callq *%r10 *******************************************************************************/ -bool patcher_checkcast_class(u1 *sp) +bool patcher_invokestatic_special(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - constant_classref *cr; - classinfo *c; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - cr = (constant_classref *) *((ptrint *) (sp + 1 * 8)); + unresolved_method* um = (unresolved_method*) pr->ref; + uintptr_t* datap = (uintptr_t*) pr->datap; - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* get the fieldinfo */ - - if (!(c = resolve_classref_eager(cr))) { - PATCHER_MONITOREXIT; + // Resolve the method. + methodinfo* m = resolve_method_eager(um); + 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 (opt_showdisassemble) - ra = ra + 5; - - /* patch super class' vftbl */ - - *((ptrint *) (ra + 2)) = (ptrint) c->vftbl; - *((ptrint *) (ra + 10 + 7 + 7 + 3 + 2)) = (ptrint) c->vftbl; - - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_instanceof_class **************************************************** +/* patcher_invokevirtual ******************************************************* Machine code: - 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10 + 4c 8b 17 mov (%rdi),%r10 + 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax + 48 ff d0 callq *%rax *******************************************************************************/ -bool patcher_instanceof_class(u1 *sp) +bool patcher_invokevirtual(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - constant_classref *cr; - classinfo *c; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - cr = (constant_classref *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_method* um = (unresolved_method*) pr->ref; - /* get the fieldinfo */ - - if (!(c = resolve_classref_eager(cr))) { - PATCHER_MONITOREXIT; + // Resovlve the method. + methodinfo* m = resolve_method_eager(um); + if (m == NULL) return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - /* if we show disassembly, we have to skip the nop's */ + pc += PATCHER_CALL_SIZE; + pc += PATCH_ALIGNMENT((uintptr_t) pc, 6, sizeof(int32_t)); - if (opt_showdisassemble) - ra = ra + 5; + // Patch vftbl index. + patch_checked_location((int32_t*) (pc + 6), (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex)); - /* patch super class' vftbl */ + // Synchronize instruction cache. + md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P); - *((ptrint *) (ra + 2)) = (ptrint) c->vftbl; - - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_clinit ************************************************************** - - May be used for GET/PUTSTATIC and in native stub. +/* patcher_invokeinterface ***************************************************** Machine code: - 4d 8b 15 92 ff ff ff mov -110(%rip),%r10 - 49 89 1a mov %rbx,(%r10) + 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_clinit(u1 *sp) +bool patcher_invokeinterface(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - classinfo *c; + uint8_t* pc = (uint8_t*) pr->mpc; + unresolved_method* um = (unresolved_method*) pr->ref; - /* get stuff from the stack */ + // Resolve the method. + methodinfo* m = resolve_method_eager(um); - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - c = (classinfo *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; + if (m == NULL) + return false; - /* check if the class is initialized */ + pc += PATCHER_CALL_SIZE; + pc += PATCH_ALIGNMENT((uintptr_t) pc, 6, sizeof(int32_t)); - if (!(c->state & CLASS_INITIALIZED)) { - if (!initialize_class(c)) { - PATCHER_MONITOREXIT; + // Patch interfacetable index. + patch_checked_location((int32_t*) (pc + 6), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index)); - return false; - } - } + int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 3 + 7), 3, sizeof(int32_t)); + pc += disp; + // Patch method offset. + int32_t *loc = patch_checked_location((int32_t*) (pc + 3 + 7 + 3), (int32_t) (sizeof(methodptr) * (m - m->clazz->methods))); - /* patch back original code */ + // Synchronize instruction cache. + checked_icache_flush(pc + 6, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P + disp, loc); - *((u8 *) ra) = mcode; - - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -/* patcher_athrow_areturn ****************************************************** +/* patcher_checkcast_interface ************************************************* Machine code: + 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 *******************************************************************************/ -#ifdef ENABLE_VERIFIER -bool patcher_athrow_areturn(u1 *sp) +bool patcher_checkcast_interface(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - unresolved_class *uc; - classinfo *c; - - /* get stuff from the stack */ + uint8_t* pc = (uint8_t*) pr->mpc; + constant_classref* cr = (constant_classref*) pr->ref; - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - uc = (unresolved_class *) *((ptrint *) (sp + 1 * 8)); + // Resolve the class. + classinfo* c = resolve_classref_eager(cr); - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; - - PATCHER_MONITORENTER; - - /* resolve the class */ + if (c == NULL) + return false; - if (!resolve_class(uc, resolveEager, false, &c)) { - PATCHER_MONITOREXIT; + pc += PATCHER_CALL_SIZE; + pc += PATCH_ALIGNMENT((uintptr_t) pc, 10, sizeof(int32_t)); - return false; - } + // Patch super class index. + patch_checked_location((int32_t*) (pc + 10), c->index); - /* patch back original code */ + int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 7 + 7 + 6 + 8), 3, sizeof(int32_t)); + pc += disp; + int32_t *loc = patch_checked_location((int32_t*) (pc + 7 + 7 + 6 + 8 + 3), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*))); - *((u8 *) ra) = mcode; + // Synchronize instruction cache. + checked_icache_flush(pc + 10, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t) + disp, loc); - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -#endif /* ENABLE_VERIFIER */ -/* patcher_resolve_native ****************************************************** +/* patcher_instanceof_interface ************************************************ Machine code: - 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax - 48 ff d0 callq *%rax + 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 *******************************************************************************/ -#if !defined(ENABLE_STATICVM) -bool patcher_resolve_native(u1 *sp) +bool patcher_instanceof_interface(patchref_t *pr) { - u1 *ra; - java_objectheader *o; - u8 mcode; - methodinfo *m; - functionptr f; - - /* get stuff from the stack */ - - ra = (u1 *) *((ptrint *) (sp + 4 * 8)); - o = (java_objectheader *) *((ptrint *) (sp + 3 * 8)); - mcode = *((u8 *) (sp + 2 * 8)); - m = (methodinfo *) *((ptrint *) (sp + 1 * 8)); - - /* calculate and set the new return address */ - - ra = ra - 5; - *((ptrint *) (sp + 4 * 8)) = (ptrint) ra; + uint8_t* pc = (uint8_t*) pr->mpc; + constant_classref* cr = (constant_classref*) pr->ref; - PATCHER_MONITORENTER; - - /* resolve native function */ - - if (!(f = native_resolve_function(m))) { - PATCHER_MONITOREXIT; + // Resolve the class. + classinfo* c = resolve_classref_eager(cr); + if (c == NULL) return false; - } - - /* patch back original code */ - - *((u8 *) ra) = mcode; - /* if we show disassembly, we have to skip the nop's */ + pc += PATCHER_CALL_SIZE; + pc += PATCH_ALIGNMENT((uintptr_t) pc, 10, sizeof(int32_t)); - if (opt_showdisassemble) - ra = ra + 5; + // Patch super class index. + patch_checked_location((int32_t*) (pc + 10), c->index); - /* patch native function pointer */ + int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 7 + 7 + 6), 3, sizeof(int32_t)); + pc += disp; + int32_t *loc = patch_checked_location((int32_t*) (pc + 7 + 7 + 6 + 3), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*))); - *((ptrint *) (ra + 2)) = (ptrint) f; + // Synchronize instruction cache. + checked_icache_flush(pc + 10, sizeof(int32_t) + 6 + 3 + sizeof(int32_t) + disp, loc); - PATCHER_MARK_PATCHED_MONITOREXIT; + // Patch back the original code. + patcher_patch_code(pr); return true; } -#endif /* !defined(ENABLE_STATICVM) */ /*