aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/stackMapTableFormat.hpp" aoqi@0: #include "interpreter/bytecodes.hpp" aoqi@0: #include "memory/metadataFactory.hpp" aoqi@0: #include "memory/oopFactory.hpp" aoqi@0: #include "memory/universe.inline.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/relocator.hpp" aoqi@0: aoqi@0: #define MAX_METHOD_LENGTH 65535 aoqi@0: aoqi@0: #define MAX_SHORT ((1 << 15) - 1) aoqi@0: #define MIN_SHORT (- (1 << 15)) aoqi@0: aoqi@0: // Encapsulates a code change request. There are 3 types. aoqi@0: // General instruction, jump instruction, and table/lookup switches aoqi@0: // aoqi@0: class ChangeItem : public ResourceObj { aoqi@0: int _bci; aoqi@0: public: aoqi@0: ChangeItem(int bci) { _bci = bci; } aoqi@0: virtual bool handle_code_change(Relocator *r) = 0; aoqi@0: aoqi@0: // type info aoqi@0: virtual bool is_widen() { return false; } aoqi@0: virtual bool is_jump_widen() { return false; } aoqi@0: virtual bool is_switch_pad() { return false; } aoqi@0: aoqi@0: // accessors aoqi@0: int bci() { return _bci; } aoqi@0: void relocate(int break_bci, int delta) { if (_bci > break_bci) { _bci += delta; } } aoqi@0: aoqi@0: virtual bool adjust(int bci, int delta) { return false; } aoqi@0: aoqi@0: // debug aoqi@0: virtual void print() = 0; aoqi@0: }; aoqi@0: aoqi@0: class ChangeWiden : public ChangeItem { aoqi@0: int _new_ilen; // New length of instruction at bci aoqi@0: u_char* _inst_buffer; // New bytecodes aoqi@0: public: aoqi@0: ChangeWiden(int bci, int new_ilen, u_char* inst_buffer) : ChangeItem(bci) { aoqi@0: _new_ilen = new_ilen; aoqi@0: _inst_buffer = inst_buffer; aoqi@0: } aoqi@0: aoqi@0: // Callback to do instruction aoqi@0: bool handle_code_change(Relocator *r) { return r->handle_widen(bci(), _new_ilen, _inst_buffer); }; aoqi@0: aoqi@0: bool is_widen() { return true; } aoqi@0: aoqi@0: void print() { tty->print_cr("ChangeWiden. bci: %d New_ilen: %d", bci(), _new_ilen); } aoqi@0: }; aoqi@0: aoqi@0: class ChangeJumpWiden : public ChangeItem { aoqi@0: int _delta; // New length of instruction at bci aoqi@0: public: aoqi@0: ChangeJumpWiden(int bci, int delta) : ChangeItem(bci) { _delta = delta; } aoqi@0: aoqi@0: // Callback to do instruction aoqi@0: bool handle_code_change(Relocator *r) { return r->handle_jump_widen(bci(), _delta); }; aoqi@0: aoqi@0: bool is_jump_widen() { return true; } aoqi@0: aoqi@0: // If the bci matches, adjust the delta in the change jump request. aoqi@0: bool adjust(int jump_bci, int delta) { aoqi@0: if (bci() == jump_bci) { aoqi@0: if (_delta > 0) aoqi@0: _delta += delta; aoqi@0: else aoqi@0: _delta -= delta; aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: void print() { tty->print_cr("ChangeJumpWiden. bci: %d Delta: %d", bci(), _delta); } aoqi@0: }; aoqi@0: aoqi@0: class ChangeSwitchPad : public ChangeItem { aoqi@0: int _padding; aoqi@0: bool _is_lookup_switch; aoqi@0: public: aoqi@0: ChangeSwitchPad(int bci, int padding, bool is_lookup_switch) : ChangeItem(bci) { aoqi@0: _padding = padding; aoqi@0: _is_lookup_switch = is_lookup_switch; aoqi@0: } aoqi@0: aoqi@0: // Callback to do instruction aoqi@0: bool handle_code_change(Relocator *r) { return r->handle_switch_pad(bci(), _padding, _is_lookup_switch); }; aoqi@0: aoqi@0: bool is_switch_pad() { return true; } aoqi@0: int padding() { return _padding; } aoqi@0: bool is_lookup_switch() { return _is_lookup_switch; } aoqi@0: aoqi@0: void print() { tty->print_cr("ChangeSwitchPad. bci: %d Padding: %d IsLookupSwitch: %d", bci(), _padding, _is_lookup_switch); } aoqi@0: }; aoqi@0: aoqi@0: //----------------------------------------------------------------------------------------------------------- aoqi@0: // Relocator code aoqi@0: aoqi@0: Relocator::Relocator(methodHandle m, RelocatorListener* listener) { aoqi@0: set_method(m); aoqi@0: set_code_length(method()->code_size()); aoqi@0: set_code_array(NULL); aoqi@0: // Allocate code array and copy bytecodes aoqi@0: if (!expand_code_array(0)) { aoqi@0: // Should have at least MAX_METHOD_LENGTH available or the verifier aoqi@0: // would have failed. aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: set_compressed_line_number_table(NULL); aoqi@0: set_compressed_line_number_table_size(0); aoqi@0: _listener = listener; aoqi@0: } aoqi@0: aoqi@0: // size is the new size of the instruction at bci. Hence, if size is less than the current aoqi@0: // instruction sice, we will shrink the code. aoqi@0: methodHandle Relocator::insert_space_at(int bci, int size, u_char inst_buffer[], TRAPS) { aoqi@0: _changes = new GrowableArray (10); aoqi@0: _changes->push(new ChangeWiden(bci, size, inst_buffer)); aoqi@0: aoqi@0: if (TraceRelocator) { aoqi@0: tty->print_cr("Space at: %d Size: %d", bci, size); aoqi@0: _method->print(); aoqi@0: _method->print_codes(); aoqi@0: tty->print_cr("-------------------------------------------------"); aoqi@0: } aoqi@0: aoqi@0: if (!handle_code_changes()) return methodHandle(); aoqi@0: aoqi@0: // Construct the new method aoqi@0: methodHandle new_method = Method::clone_with_new_data(method(), aoqi@0: code_array(), code_length(), aoqi@0: compressed_line_number_table(), aoqi@0: compressed_line_number_table_size(), aoqi@0: CHECK_(methodHandle())); aoqi@0: aoqi@0: // Deallocate the old Method* from metadata aoqi@0: ClassLoaderData* loader_data = method()->method_holder()->class_loader_data(); aoqi@0: loader_data->add_to_deallocate_list(method()()); aoqi@0: aoqi@0: set_method(new_method); aoqi@0: aoqi@0: if (TraceRelocator) { aoqi@0: tty->print_cr("-------------------------------------------------"); aoqi@0: tty->print_cr("new method"); aoqi@0: _method->print_codes(); aoqi@0: } aoqi@0: aoqi@0: return new_method; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool Relocator::handle_code_changes() { aoqi@0: assert(_changes != NULL, "changes vector must be initialized"); aoqi@0: aoqi@0: while (!_changes->is_empty()) { aoqi@0: // Inv: everything is aligned. aoqi@0: ChangeItem* ci = _changes->first(); aoqi@0: aoqi@0: if (TraceRelocator) { aoqi@0: ci->print(); aoqi@0: } aoqi@0: aoqi@0: // Execute operation aoqi@0: if (!ci->handle_code_change(this)) return false; aoqi@0: aoqi@0: // Shuffel items up aoqi@0: for (int index = 1; index < _changes->length(); index++) { aoqi@0: _changes->at_put(index-1, _changes->at(index)); aoqi@0: } aoqi@0: _changes->pop(); aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool Relocator::is_opcode_lookupswitch(Bytecodes::Code bc) { aoqi@0: switch (bc) { aoqi@0: case Bytecodes::_tableswitch: return false; aoqi@0: case Bytecodes::_lookupswitch: // not rewritten on ia64 aoqi@0: case Bytecodes::_fast_linearswitch: // rewritten _lookupswitch aoqi@0: case Bytecodes::_fast_binaryswitch: return true; // rewritten _lookupswitch aoqi@0: default: ShouldNotReachHere(); aoqi@0: } aoqi@0: return true; // dummy aoqi@0: } aoqi@0: aoqi@0: // We need a special instruction size method, since lookupswitches and tableswitches might not be aoqi@0: // properly alligned during relocation aoqi@0: int Relocator::rc_instr_len(int bci) { aoqi@0: Bytecodes::Code bc= code_at(bci); aoqi@0: switch (bc) { aoqi@0: // In the case of switch instructions, see if we have the original aoqi@0: // padding recorded. aoqi@0: case Bytecodes::_tableswitch: aoqi@0: case Bytecodes::_lookupswitch: aoqi@0: case Bytecodes::_fast_linearswitch: aoqi@0: case Bytecodes::_fast_binaryswitch: aoqi@0: { aoqi@0: int pad = get_orig_switch_pad(bci, is_opcode_lookupswitch(bc)); aoqi@0: if (pad == -1) { aoqi@0: return instruction_length_at(bci); aoqi@0: } aoqi@0: // Otherwise, depends on the switch type. aoqi@0: switch (bc) { aoqi@0: case Bytecodes::_tableswitch: { aoqi@0: int lo = int_at(bci + 1 + pad + 4 * 1); aoqi@0: int hi = int_at(bci + 1 + pad + 4 * 2); aoqi@0: int n = hi - lo + 1; aoqi@0: return 1 + pad + 4*(3 + n); aoqi@0: } aoqi@0: case Bytecodes::_lookupswitch: aoqi@0: case Bytecodes::_fast_linearswitch: aoqi@0: case Bytecodes::_fast_binaryswitch: { aoqi@0: int npairs = int_at(bci + 1 + pad + 4 * 1); aoqi@0: return 1 + pad + 4*(2 + 2*npairs); aoqi@0: } aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return instruction_length_at(bci); aoqi@0: } aoqi@0: aoqi@0: // If a change item is recorded for "pc", with type "ct", returns the aoqi@0: // associated padding, else -1. aoqi@0: int Relocator::get_orig_switch_pad(int bci, bool is_lookup_switch) { aoqi@0: for (int k = 0; k < _changes->length(); k++) { aoqi@0: ChangeItem* ci = _changes->at(k); aoqi@0: if (ci->is_switch_pad()) { aoqi@0: ChangeSwitchPad* csp = (ChangeSwitchPad*)ci; aoqi@0: if (csp->is_lookup_switch() == is_lookup_switch && csp->bci() == bci) { aoqi@0: return csp->padding(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Push a ChangeJumpWiden if it doesn't already exist on the work queue, aoqi@0: // otherwise adjust the item already there by delta. The calculation for aoqi@0: // new_delta is wrong for this because it uses the offset stored in the aoqi@0: // code stream itself which wasn't fixed when item was pushed on the work queue. aoqi@0: void Relocator::push_jump_widen(int bci, int delta, int new_delta) { aoqi@0: for (int j = 0; j < _changes->length(); j++) { aoqi@0: ChangeItem* ci = _changes->at(j); aoqi@0: if (ci->adjust(bci, delta)) return; aoqi@0: } aoqi@0: _changes->push(new ChangeJumpWiden(bci, new_delta)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // The current instruction of "c" is a jump; one of its offset starts aoqi@0: // at "offset" and is a short if "isShort" is "TRUE", aoqi@0: // and an integer otherwise. If the jump crosses "breakPC", change aoqi@0: // the span of the jump by "delta". aoqi@0: void Relocator::change_jump(int bci, int offset, bool is_short, int break_bci, int delta) { aoqi@0: int bci_delta = (is_short) ? short_at(offset) : int_at(offset); aoqi@0: int targ = bci + bci_delta; aoqi@0: aoqi@0: if ((bci <= break_bci && targ > break_bci) || aoqi@0: (bci > break_bci && targ <= break_bci)) { aoqi@0: int new_delta; aoqi@0: if (bci_delta > 0) aoqi@0: new_delta = bci_delta + delta; aoqi@0: else aoqi@0: new_delta = bci_delta - delta; aoqi@0: aoqi@0: if (is_short && ((new_delta > MAX_SHORT) || new_delta < MIN_SHORT)) { aoqi@0: push_jump_widen(bci, delta, new_delta); aoqi@0: } else if (is_short) { aoqi@0: short_at_put(offset, new_delta); aoqi@0: } else { aoqi@0: int_at_put(offset, new_delta); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Changes all jumps crossing "break_bci" by "delta". May enqueue things aoqi@0: // on "rc->changes" aoqi@0: void Relocator::change_jumps(int break_bci, int delta) { aoqi@0: int bci = 0; aoqi@0: Bytecodes::Code bc; aoqi@0: // Now, adjust any affected instructions. aoqi@0: while (bci < code_length()) { aoqi@0: switch (bc= code_at(bci)) { aoqi@0: case Bytecodes::_ifeq: aoqi@0: case Bytecodes::_ifne: aoqi@0: case Bytecodes::_iflt: aoqi@0: case Bytecodes::_ifge: aoqi@0: case Bytecodes::_ifgt: aoqi@0: case Bytecodes::_ifle: aoqi@0: case Bytecodes::_if_icmpeq: aoqi@0: case Bytecodes::_if_icmpne: aoqi@0: case Bytecodes::_if_icmplt: aoqi@0: case Bytecodes::_if_icmpge: aoqi@0: case Bytecodes::_if_icmpgt: aoqi@0: case Bytecodes::_if_icmple: aoqi@0: case Bytecodes::_if_acmpeq: aoqi@0: case Bytecodes::_if_acmpne: aoqi@0: case Bytecodes::_ifnull: aoqi@0: case Bytecodes::_ifnonnull: aoqi@0: case Bytecodes::_goto: aoqi@0: case Bytecodes::_jsr: aoqi@0: change_jump(bci, bci+1, true, break_bci, delta); aoqi@0: break; aoqi@0: case Bytecodes::_goto_w: aoqi@0: case Bytecodes::_jsr_w: aoqi@0: change_jump(bci, bci+1, false, break_bci, delta); aoqi@0: break; aoqi@0: case Bytecodes::_tableswitch: aoqi@0: case Bytecodes::_lookupswitch: aoqi@0: case Bytecodes::_fast_linearswitch: aoqi@0: case Bytecodes::_fast_binaryswitch: { aoqi@0: int recPad = get_orig_switch_pad(bci, (bc != Bytecodes::_tableswitch)); aoqi@0: int oldPad = (recPad != -1) ? recPad : align(bci+1) - (bci+1); aoqi@0: if (bci > break_bci) { aoqi@0: int new_bci = bci + delta; aoqi@0: int newPad = align(new_bci+1) - (new_bci+1); aoqi@0: // Do we need to check the padding? aoqi@0: if (newPad != oldPad) { aoqi@0: if (recPad == -1) { aoqi@0: _changes->push(new ChangeSwitchPad(bci, oldPad, (bc != Bytecodes::_tableswitch))); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Then the rest, which depend on the kind of switch. aoqi@0: switch (bc) { aoqi@0: case Bytecodes::_tableswitch: { aoqi@0: change_jump(bci, bci +1 + oldPad, false, break_bci, delta); aoqi@0: // We cannot use the Bytecode_tableswitch abstraction, since the padding might not be correct. aoqi@0: int lo = int_at(bci + 1 + oldPad + 4 * 1); aoqi@0: int hi = int_at(bci + 1 + oldPad + 4 * 2); aoqi@0: int n = hi - lo + 1; aoqi@0: for (int k = 0; k < n; k++) { aoqi@0: change_jump(bci, bci +1 + oldPad + 4*(k+3), false, break_bci, delta); aoqi@0: } aoqi@0: // Special next-bci calculation here... aoqi@0: bci += 1 + oldPad + (n+3)*4; aoqi@0: continue; aoqi@0: } aoqi@0: case Bytecodes::_lookupswitch: aoqi@0: case Bytecodes::_fast_linearswitch: aoqi@0: case Bytecodes::_fast_binaryswitch: { aoqi@0: change_jump(bci, bci +1 + oldPad, false, break_bci, delta); aoqi@0: // We cannot use the Bytecode_lookupswitch abstraction, since the padding might not be correct. aoqi@0: int npairs = int_at(bci + 1 + oldPad + 4 * 1); aoqi@0: for (int k = 0; k < npairs; k++) { aoqi@0: change_jump(bci, bci + 1 + oldPad + 4*(2 + 2*k + 1), false, break_bci, delta); aoqi@0: } aoqi@0: /* Special next-bci calculation here... */ aoqi@0: bci += 1 + oldPad + (2 + (npairs*2))*4; aoqi@0: continue; aoqi@0: } aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: default: aoqi@0: break; aoqi@0: } aoqi@0: bci += rc_instr_len(bci); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // The width of instruction at "pc" is changing by "delta". Adjust the aoqi@0: // exception table, if any, of "rc->mb". aoqi@0: void Relocator::adjust_exception_table(int bci, int delta) { aoqi@0: ExceptionTable table(_method()); aoqi@0: for (int index = 0; index < table.length(); index ++) { aoqi@0: if (table.start_pc(index) > bci) { aoqi@0: table.set_start_pc(index, table.start_pc(index) + delta); aoqi@0: table.set_end_pc(index, table.end_pc(index) + delta); aoqi@0: } else if (bci < table.end_pc(index)) { aoqi@0: table.set_end_pc(index, table.end_pc(index) + delta); aoqi@0: } aoqi@0: if (table.handler_pc(index) > bci) aoqi@0: table.set_handler_pc(index, table.handler_pc(index) + delta); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // The width of instruction at "bci" is changing by "delta". Adjust the line number table. aoqi@0: void Relocator::adjust_line_no_table(int bci, int delta) { aoqi@0: if (method()->has_linenumber_table()) { aoqi@0: CompressedLineNumberReadStream reader(method()->compressed_linenumber_table()); aoqi@0: CompressedLineNumberWriteStream writer(64); // plenty big for most line number tables aoqi@0: while (reader.read_pair()) { aoqi@0: int adjustment = (reader.bci() > bci) ? delta : 0; aoqi@0: writer.write_pair(reader.bci() + adjustment, reader.line()); aoqi@0: } aoqi@0: writer.write_terminator(); aoqi@0: set_compressed_line_number_table(writer.buffer()); aoqi@0: set_compressed_line_number_table_size(writer.position()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // The width of instruction at "bci" is changing by "delta". Adjust the local variable table. aoqi@0: void Relocator::adjust_local_var_table(int bci, int delta) { aoqi@0: int localvariable_table_length = method()->localvariable_table_length(); aoqi@0: if (localvariable_table_length > 0) { aoqi@0: LocalVariableTableElement* table = method()->localvariable_table_start(); aoqi@0: for (int i = 0; i < localvariable_table_length; i++) { aoqi@0: u2 current_bci = table[i].start_bci; aoqi@0: if (current_bci > bci) { aoqi@0: table[i].start_bci = current_bci + delta; aoqi@0: } else { aoqi@0: u2 current_length = table[i].length; aoqi@0: if (current_bci + current_length > bci) { aoqi@0: table[i].length = current_length + delta; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Create a new array, copying the src array but adding a hole at aoqi@0: // the specified location aoqi@0: static Array* insert_hole_at(ClassLoaderData* loader_data, aoqi@0: size_t where, int hole_sz, Array* src) { aoqi@0: Thread* THREAD = Thread::current(); aoqi@0: Array* dst = aoqi@0: MetadataFactory::new_array(loader_data, src->length() + hole_sz, 0, CHECK_NULL); aoqi@0: aoqi@0: address src_addr = (address)src->adr_at(0); aoqi@0: address dst_addr = (address)dst->adr_at(0); aoqi@0: aoqi@0: memcpy(dst_addr, src_addr, where); aoqi@0: memcpy(dst_addr + where + hole_sz, aoqi@0: src_addr + where, src->length() - where); aoqi@0: return dst; aoqi@0: } aoqi@0: aoqi@0: // The width of instruction at "bci" is changing by "delta". Adjust the stack aoqi@0: // map frames. aoqi@0: void Relocator::adjust_stack_map_table(int bci, int delta) { aoqi@0: if (method()->has_stackmap_table()) { aoqi@0: Array* data = method()->stackmap_data(); aoqi@0: // The data in the array is a classfile representation of the stackmap table aoqi@0: stack_map_table* sm_table = aoqi@0: stack_map_table::at((address)data->adr_at(0)); aoqi@0: aoqi@0: int count = sm_table->number_of_entries(); aoqi@0: stack_map_frame* frame = sm_table->entries(); aoqi@0: int bci_iter = -1; aoqi@0: bool offset_adjusted = false; // only need to adjust one offset aoqi@0: aoqi@0: for (int i = 0; i < count; ++i) { aoqi@0: int offset_delta = frame->offset_delta(); aoqi@0: bci_iter += offset_delta; aoqi@0: aoqi@0: if (!offset_adjusted && bci_iter > bci) { aoqi@0: int new_offset_delta = offset_delta + delta; aoqi@0: aoqi@0: if (frame->is_valid_offset(new_offset_delta)) { aoqi@0: frame->set_offset_delta(new_offset_delta); aoqi@0: } else { aoqi@0: assert(frame->is_same_frame() || aoqi@0: frame->is_same_locals_1_stack_item_frame(), aoqi@0: "Frame must be one of the compressed forms"); aoqi@0: // The new delta exceeds the capacity of the 'same_frame' or aoqi@0: // 'same_frame_1_stack_item_frame' frame types. We need to aoqi@0: // convert these frames to the extended versions, but the extended aoqi@0: // version is bigger and requires more room. So we allocate a aoqi@0: // new array and copy the data, being sure to leave u2-sized hole aoqi@0: // right after the 'frame_type' for the new offset field. aoqi@0: // aoqi@0: // We can safely ignore the reverse situation as a small delta aoqi@0: // can still be used in an extended version of the frame. aoqi@0: aoqi@0: size_t frame_offset = (address)frame - (address)data->adr_at(0); aoqi@0: aoqi@0: ClassLoaderData* loader_data = method()->method_holder()->class_loader_data(); aoqi@0: Array* new_data = insert_hole_at(loader_data, frame_offset + 1, 2, data); aoqi@0: if (new_data == NULL) { aoqi@0: return; // out-of-memory? aoqi@0: } aoqi@0: // Deallocate old data aoqi@0: MetadataFactory::free_array(loader_data, data); aoqi@0: data = new_data; aoqi@0: aoqi@0: address frame_addr = (address)(data->adr_at(0) + frame_offset); aoqi@0: frame = stack_map_frame::at(frame_addr); aoqi@0: aoqi@0: aoqi@0: // Now convert the frames in place aoqi@0: if (frame->is_same_frame()) { aoqi@0: same_frame_extended::create_at(frame_addr, new_offset_delta); aoqi@0: } else { aoqi@0: same_locals_1_stack_item_extended::create_at( aoqi@0: frame_addr, new_offset_delta, NULL); aoqi@0: // the verification_info_type should already be at the right spot aoqi@0: } aoqi@0: } aoqi@0: offset_adjusted = true; // needs to be done only once, since subsequent aoqi@0: // values are offsets from the current aoqi@0: } aoqi@0: aoqi@0: // The stack map frame may contain verification types, if so we need to aoqi@0: // check and update any Uninitialized type's bci (no matter where it is). aoqi@0: int number_of_types = frame->number_of_types(); aoqi@0: verification_type_info* types = frame->types(); aoqi@0: aoqi@0: for (int i = 0; i < number_of_types; ++i) { aoqi@0: if (types->is_uninitialized() && types->bci() > bci) { aoqi@0: types->set_bci(types->bci() + delta); aoqi@0: } aoqi@0: types = types->next(); aoqi@0: } aoqi@0: aoqi@0: // Full frame has stack values too aoqi@0: full_frame* ff = frame->as_full_frame(); aoqi@0: if (ff != NULL) { aoqi@0: address eol = (address)types; aoqi@0: number_of_types = ff->stack_slots(eol); aoqi@0: types = ff->stack(eol); aoqi@0: for (int i = 0; i < number_of_types; ++i) { aoqi@0: if (types->is_uninitialized() && types->bci() > bci) { aoqi@0: types->set_bci(types->bci() + delta); aoqi@0: } aoqi@0: types = types->next(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: frame = frame->next(); aoqi@0: } aoqi@0: aoqi@0: method()->set_stackmap_data(data); // in case it has changed aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool Relocator::expand_code_array(int delta) { aoqi@0: int length = MAX2(code_length() + delta, code_length() * (100+code_slop_pct()) / 100); aoqi@0: aoqi@0: if (length > MAX_METHOD_LENGTH) { aoqi@0: if (delta == 0 && code_length() <= MAX_METHOD_LENGTH) { aoqi@0: length = MAX_METHOD_LENGTH; aoqi@0: } else { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: unsigned char* new_code_array = NEW_RESOURCE_ARRAY(unsigned char, length); aoqi@0: if (!new_code_array) return false; aoqi@0: aoqi@0: // Expanding current array aoqi@0: if (code_array() != NULL) { aoqi@0: memcpy(new_code_array, code_array(), code_length()); aoqi@0: } else { aoqi@0: // Initial copy. Copy directly from Method* aoqi@0: memcpy(new_code_array, method()->code_base(), code_length()); aoqi@0: } aoqi@0: aoqi@0: set_code_array(new_code_array); aoqi@0: set_code_array_length(length); aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // The instruction at "bci", whose size is "ilen", is changing size by aoqi@0: // "delta". Reallocate, move code, recalculate jumps, and enqueue aoqi@0: // change items as necessary. aoqi@0: bool Relocator::relocate_code(int bci, int ilen, int delta) { aoqi@0: int next_bci = bci + ilen; aoqi@0: if (delta > 0 && code_length() + delta > code_array_length()) { aoqi@0: // Expand allocated code space, if necessary. aoqi@0: if (!expand_code_array(delta)) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We require 4-byte alignment of code arrays. aoqi@0: assert(((intptr_t)code_array() & 3) == 0, "check code alignment"); aoqi@0: // Change jumps before doing the copying; this routine requires aligned switches. aoqi@0: change_jumps(bci, delta); aoqi@0: aoqi@0: // In case we have shrunken a tableswitch/lookupswitch statement, we store the last aoqi@0: // bytes that get overwritten. We have to copy the bytes after the change_jumps method aoqi@0: // has been called, since it is likly to update last offset in a tableswitch/lookupswitch aoqi@0: if (delta < 0) { aoqi@0: assert(delta>=-3, "we cannot overwrite more than 3 bytes"); aoqi@0: memcpy(_overwrite, addr_at(bci + ilen + delta), -delta); aoqi@0: } aoqi@0: aoqi@0: memmove(addr_at(next_bci + delta), addr_at(next_bci), code_length() - next_bci); aoqi@0: set_code_length(code_length() + delta); aoqi@0: // Also adjust exception tables... aoqi@0: adjust_exception_table(bci, delta); aoqi@0: // Line number tables... aoqi@0: adjust_line_no_table(bci, delta); aoqi@0: // And local variable table... aoqi@0: adjust_local_var_table(bci, delta); aoqi@0: aoqi@0: // Adjust stack maps aoqi@0: adjust_stack_map_table(bci, delta); aoqi@0: aoqi@0: // Relocate the pending change stack... aoqi@0: for (int j = 0; j < _changes->length(); j++) { aoqi@0: ChangeItem* ci = _changes->at(j); aoqi@0: ci->relocate(bci, delta); aoqi@0: } aoqi@0: aoqi@0: // Notify any listeners about code relocation aoqi@0: notify(bci, delta, code_length()); aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // relocate a general instruction. Called by ChangeWiden class aoqi@0: bool Relocator::handle_widen(int bci, int new_ilen, u_char inst_buffer[]) { aoqi@0: int ilen = rc_instr_len(bci); aoqi@0: if (!relocate_code(bci, ilen, new_ilen - ilen)) aoqi@0: return false; aoqi@0: aoqi@0: // Insert new bytecode(s) aoqi@0: for(int k = 0; k < new_ilen; k++) { aoqi@0: code_at_put(bci + k, (Bytecodes::Code)inst_buffer[k]); aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // handle jump_widen instruction. Called be ChangeJumpWiden class aoqi@0: bool Relocator::handle_jump_widen(int bci, int delta) { aoqi@0: int ilen = rc_instr_len(bci); aoqi@0: aoqi@0: Bytecodes::Code bc = code_at(bci); aoqi@0: switch (bc) { aoqi@0: case Bytecodes::_ifeq: aoqi@0: case Bytecodes::_ifne: aoqi@0: case Bytecodes::_iflt: aoqi@0: case Bytecodes::_ifge: aoqi@0: case Bytecodes::_ifgt: aoqi@0: case Bytecodes::_ifle: aoqi@0: case Bytecodes::_if_icmpeq: aoqi@0: case Bytecodes::_if_icmpne: aoqi@0: case Bytecodes::_if_icmplt: aoqi@0: case Bytecodes::_if_icmpge: aoqi@0: case Bytecodes::_if_icmpgt: aoqi@0: case Bytecodes::_if_icmple: aoqi@0: case Bytecodes::_if_acmpeq: aoqi@0: case Bytecodes::_if_acmpne: aoqi@0: case Bytecodes::_ifnull: aoqi@0: case Bytecodes::_ifnonnull: { aoqi@0: const int goto_length = Bytecodes::length_for(Bytecodes::_goto); aoqi@0: aoqi@0: // If 'if' points to the next bytecode after goto, it's already handled. aoqi@0: // it shouldn't be. aoqi@0: assert (short_at(bci+1) != ilen+goto_length, "if relocation already handled"); aoqi@0: assert(ilen == 3, "check length"); aoqi@0: aoqi@0: // Convert to 0 if goto 6 aoqi@0: // 3 _goto 11 aoqi@0: // 6 _goto_w aoqi@0: // 11 aoqi@0: const int goto_w_length = Bytecodes::length_for(Bytecodes::_goto_w); aoqi@0: const int add_bci = goto_length + goto_w_length; aoqi@0: aoqi@0: if (!relocate_code(bci, 3, /*delta*/add_bci)) return false; aoqi@0: aoqi@0: // if bytecode points to goto_w instruction aoqi@0: short_at_put(bci + 1, ilen + goto_length); aoqi@0: aoqi@0: int cbci = bci + ilen; aoqi@0: // goto around aoqi@0: code_at_put(cbci, Bytecodes::_goto); aoqi@0: short_at_put(cbci + 1, add_bci); aoqi@0: // goto_w aoqi@0: cbci = cbci + goto_length; aoqi@0: code_at_put(cbci, Bytecodes::_goto_w); aoqi@0: if (delta > 0) { aoqi@0: delta += 2; // goto_w is 2 bytes more than "if" code aoqi@0: } else { aoqi@0: delta -= ilen+goto_length; // branch starts at goto_w offset aoqi@0: } aoqi@0: int_at_put(cbci + 1, delta); aoqi@0: break; aoqi@0: aoqi@0: } aoqi@0: case Bytecodes::_goto: aoqi@0: case Bytecodes::_jsr: aoqi@0: assert(ilen == 3, "check length"); aoqi@0: aoqi@0: if (!relocate_code(bci, 3, 2)) return false; aoqi@0: if (bc == Bytecodes::_goto) aoqi@0: code_at_put(bci, Bytecodes::_goto_w); aoqi@0: else aoqi@0: code_at_put(bci, Bytecodes::_jsr_w); aoqi@0: aoqi@0: // If it's a forward jump, add 2 for the widening. aoqi@0: if (delta > 0) delta += 2; aoqi@0: int_at_put(bci + 1, delta); aoqi@0: break; aoqi@0: aoqi@0: default: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // handle lookup/table switch instructions. Called be ChangeSwitchPad class aoqi@0: bool Relocator::handle_switch_pad(int bci, int old_pad, bool is_lookup_switch) { aoqi@0: int ilen = rc_instr_len(bci); aoqi@0: int new_pad = align(bci+1) - (bci+1); aoqi@0: int pad_delta = new_pad - old_pad; aoqi@0: if (pad_delta != 0) { aoqi@0: int len; aoqi@0: if (!is_lookup_switch) { aoqi@0: int low = int_at(bci+1+old_pad+4); aoqi@0: int high = int_at(bci+1+old_pad+8); aoqi@0: len = high-low+1 + 3; // 3 for default, hi, lo. aoqi@0: } else { aoqi@0: int npairs = int_at(bci+1+old_pad+4); aoqi@0: len = npairs*2 + 2; // 2 for default, npairs. aoqi@0: } aoqi@0: // Because "relocateCode" does a "changeJumps" loop, aoqi@0: // which parses instructions to determine their length, aoqi@0: // we need to call that before messing with the current aoqi@0: // instruction. Since it may also overwrite the current aoqi@0: // instruction when moving down, remember the possibly aoqi@0: // overwritten part. aoqi@0: aoqi@0: // Move the code following the instruction... aoqi@0: if (!relocate_code(bci, ilen, pad_delta)) return false; aoqi@0: aoqi@0: if (pad_delta < 0) { aoqi@0: // Move the shrunken instruction down. aoqi@0: memmove(addr_at(bci + 1 + new_pad), aoqi@0: addr_at(bci + 1 + old_pad), aoqi@0: len * 4 + pad_delta); aoqi@0: memmove(addr_at(bci + 1 + new_pad + len*4 + pad_delta), aoqi@0: _overwrite, -pad_delta); aoqi@0: } else { aoqi@0: assert(pad_delta > 0, "check"); aoqi@0: // Move the expanded instruction up. aoqi@0: memmove(addr_at(bci +1 + new_pad), aoqi@0: addr_at(bci +1 + old_pad), aoqi@0: len * 4); aoqi@0: memset(addr_at(bci + 1), 0, new_pad); // pad must be 0 aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: }