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