src/share/vm/runtime/relocator.cpp

Fri, 28 Mar 2014 10:13:37 -0700

author
vlivanov
date
Fri, 28 Mar 2014 10:13:37 -0700
changeset 6528
248ff38d2950
parent 4037
da91efe96a93
child 6876
710a3c8b516e
permissions
-rw-r--r--

8035828: Turn on @Stable support in VM
Reviewed-by: jrose, twisti

duke@435 1 /*
jiangli@3917 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/stackMapTableFormat.hpp"
stefank@2314 27 #include "interpreter/bytecodes.hpp"
coleenp@4037 28 #include "memory/metadataFactory.hpp"
stefank@2314 29 #include "memory/oopFactory.hpp"
stefank@2314 30 #include "memory/universe.inline.hpp"
stefank@2314 31 #include "oops/oop.inline.hpp"
stefank@2314 32 #include "runtime/handles.inline.hpp"
stefank@2314 33 #include "runtime/relocator.hpp"
duke@435 34
duke@435 35 #define MAX_METHOD_LENGTH 65535
duke@435 36
duke@435 37 #define MAX_SHORT ((1 << 15) - 1)
duke@435 38 #define MIN_SHORT (- (1 << 15))
duke@435 39
duke@435 40 // Encapsulates a code change request. There are 3 types.
duke@435 41 // General instruction, jump instruction, and table/lookup switches
duke@435 42 //
duke@435 43 class ChangeItem : public ResourceObj {
duke@435 44 int _bci;
duke@435 45 public:
duke@435 46 ChangeItem(int bci) { _bci = bci; }
duke@435 47 virtual bool handle_code_change(Relocator *r) = 0;
duke@435 48
duke@435 49 // type info
duke@435 50 virtual bool is_widen() { return false; }
duke@435 51 virtual bool is_jump_widen() { return false; }
duke@435 52 virtual bool is_switch_pad() { return false; }
duke@435 53
duke@435 54 // accessors
duke@435 55 int bci() { return _bci; }
duke@435 56 void relocate(int break_bci, int delta) { if (_bci > break_bci) { _bci += delta; } }
duke@435 57
duke@435 58 virtual bool adjust(int bci, int delta) { return false; }
duke@435 59
duke@435 60 // debug
duke@435 61 virtual void print() = 0;
duke@435 62 };
duke@435 63
duke@435 64 class ChangeWiden : public ChangeItem {
duke@435 65 int _new_ilen; // New length of instruction at bci
duke@435 66 u_char* _inst_buffer; // New bytecodes
duke@435 67 public:
duke@435 68 ChangeWiden(int bci, int new_ilen, u_char* inst_buffer) : ChangeItem(bci) {
duke@435 69 _new_ilen = new_ilen;
duke@435 70 _inst_buffer = inst_buffer;
duke@435 71 }
duke@435 72
duke@435 73 // Callback to do instruction
duke@435 74 bool handle_code_change(Relocator *r) { return r->handle_widen(bci(), _new_ilen, _inst_buffer); };
duke@435 75
duke@435 76 bool is_widen() { return true; }
duke@435 77
duke@435 78 void print() { tty->print_cr("ChangeWiden. bci: %d New_ilen: %d", bci(), _new_ilen); }
duke@435 79 };
duke@435 80
duke@435 81 class ChangeJumpWiden : public ChangeItem {
duke@435 82 int _delta; // New length of instruction at bci
duke@435 83 public:
duke@435 84 ChangeJumpWiden(int bci, int delta) : ChangeItem(bci) { _delta = delta; }
duke@435 85
duke@435 86 // Callback to do instruction
duke@435 87 bool handle_code_change(Relocator *r) { return r->handle_jump_widen(bci(), _delta); };
duke@435 88
duke@435 89 bool is_jump_widen() { return true; }
duke@435 90
duke@435 91 // If the bci matches, adjust the delta in the change jump request.
duke@435 92 bool adjust(int jump_bci, int delta) {
duke@435 93 if (bci() == jump_bci) {
duke@435 94 if (_delta > 0)
duke@435 95 _delta += delta;
duke@435 96 else
duke@435 97 _delta -= delta;
duke@435 98 return true;
duke@435 99 }
duke@435 100 return false;
duke@435 101 }
duke@435 102
duke@435 103 void print() { tty->print_cr("ChangeJumpWiden. bci: %d Delta: %d", bci(), _delta); }
duke@435 104 };
duke@435 105
duke@435 106 class ChangeSwitchPad : public ChangeItem {
duke@435 107 int _padding;
duke@435 108 bool _is_lookup_switch;
duke@435 109 public:
duke@435 110 ChangeSwitchPad(int bci, int padding, bool is_lookup_switch) : ChangeItem(bci) {
duke@435 111 _padding = padding;
duke@435 112 _is_lookup_switch = is_lookup_switch;
duke@435 113 }
duke@435 114
duke@435 115 // Callback to do instruction
duke@435 116 bool handle_code_change(Relocator *r) { return r->handle_switch_pad(bci(), _padding, _is_lookup_switch); };
duke@435 117
duke@435 118 bool is_switch_pad() { return true; }
duke@435 119 int padding() { return _padding; }
duke@435 120 bool is_lookup_switch() { return _is_lookup_switch; }
duke@435 121
duke@435 122 void print() { tty->print_cr("ChangeSwitchPad. bci: %d Padding: %d IsLookupSwitch: %d", bci(), _padding, _is_lookup_switch); }
duke@435 123 };
duke@435 124
duke@435 125 //-----------------------------------------------------------------------------------------------------------
duke@435 126 // Relocator code
duke@435 127
duke@435 128 Relocator::Relocator(methodHandle m, RelocatorListener* listener) {
duke@435 129 set_method(m);
duke@435 130 set_code_length(method()->code_size());
duke@435 131 set_code_array(NULL);
duke@435 132 // Allocate code array and copy bytecodes
duke@435 133 if (!expand_code_array(0)) {
duke@435 134 // Should have at least MAX_METHOD_LENGTH available or the verifier
duke@435 135 // would have failed.
duke@435 136 ShouldNotReachHere();
duke@435 137 }
duke@435 138 set_compressed_line_number_table(NULL);
duke@435 139 set_compressed_line_number_table_size(0);
duke@435 140 _listener = listener;
duke@435 141 }
duke@435 142
duke@435 143 // size is the new size of the instruction at bci. Hence, if size is less than the current
duke@435 144 // instruction sice, we will shrink the code.
duke@435 145 methodHandle Relocator::insert_space_at(int bci, int size, u_char inst_buffer[], TRAPS) {
duke@435 146 _changes = new GrowableArray<ChangeItem*> (10);
duke@435 147 _changes->push(new ChangeWiden(bci, size, inst_buffer));
duke@435 148
duke@435 149 if (TraceRelocator) {
duke@435 150 tty->print_cr("Space at: %d Size: %d", bci, size);
duke@435 151 _method->print();
duke@435 152 _method->print_codes();
duke@435 153 tty->print_cr("-------------------------------------------------");
duke@435 154 }
duke@435 155
duke@435 156 if (!handle_code_changes()) return methodHandle();
duke@435 157
duke@435 158 // Construct the new method
coleenp@4037 159 methodHandle new_method = Method::clone_with_new_data(method(),
duke@435 160 code_array(), code_length(),
duke@435 161 compressed_line_number_table(),
duke@435 162 compressed_line_number_table_size(),
duke@435 163 CHECK_(methodHandle()));
coleenp@4037 164
coleenp@4037 165 // Deallocate the old Method* from metadata
coleenp@4037 166 ClassLoaderData* loader_data = method()->method_holder()->class_loader_data();
coleenp@4037 167 loader_data->add_to_deallocate_list(method()());
coleenp@4037 168
duke@435 169 set_method(new_method);
duke@435 170
duke@435 171 if (TraceRelocator) {
duke@435 172 tty->print_cr("-------------------------------------------------");
duke@435 173 tty->print_cr("new method");
duke@435 174 _method->print_codes();
duke@435 175 }
duke@435 176
duke@435 177 return new_method;
duke@435 178 }
duke@435 179
duke@435 180
duke@435 181 bool Relocator::handle_code_changes() {
duke@435 182 assert(_changes != NULL, "changes vector must be initialized");
duke@435 183
duke@435 184 while (!_changes->is_empty()) {
duke@435 185 // Inv: everything is aligned.
duke@435 186 ChangeItem* ci = _changes->first();
duke@435 187
duke@435 188 if (TraceRelocator) {
duke@435 189 ci->print();
duke@435 190 }
duke@435 191
duke@435 192 // Execute operation
duke@435 193 if (!ci->handle_code_change(this)) return false;
duke@435 194
duke@435 195 // Shuffel items up
duke@435 196 for (int index = 1; index < _changes->length(); index++) {
duke@435 197 _changes->at_put(index-1, _changes->at(index));
duke@435 198 }
duke@435 199 _changes->pop();
duke@435 200 }
duke@435 201 return true;
duke@435 202 }
duke@435 203
duke@435 204
duke@435 205 bool Relocator::is_opcode_lookupswitch(Bytecodes::Code bc) {
duke@435 206 switch (bc) {
duke@435 207 case Bytecodes::_tableswitch: return false;
duke@435 208 case Bytecodes::_lookupswitch: // not rewritten on ia64
duke@435 209 case Bytecodes::_fast_linearswitch: // rewritten _lookupswitch
duke@435 210 case Bytecodes::_fast_binaryswitch: return true; // rewritten _lookupswitch
duke@435 211 default: ShouldNotReachHere();
duke@435 212 }
duke@435 213 return true; // dummy
duke@435 214 }
duke@435 215
duke@435 216 // We need a special instruction size method, since lookupswitches and tableswitches might not be
duke@435 217 // properly alligned during relocation
duke@435 218 int Relocator::rc_instr_len(int bci) {
duke@435 219 Bytecodes::Code bc= code_at(bci);
duke@435 220 switch (bc) {
duke@435 221 // In the case of switch instructions, see if we have the original
duke@435 222 // padding recorded.
duke@435 223 case Bytecodes::_tableswitch:
duke@435 224 case Bytecodes::_lookupswitch:
duke@435 225 case Bytecodes::_fast_linearswitch:
duke@435 226 case Bytecodes::_fast_binaryswitch:
duke@435 227 {
duke@435 228 int pad = get_orig_switch_pad(bci, is_opcode_lookupswitch(bc));
duke@435 229 if (pad == -1) {
duke@435 230 return instruction_length_at(bci);
duke@435 231 }
duke@435 232 // Otherwise, depends on the switch type.
duke@435 233 switch (bc) {
duke@435 234 case Bytecodes::_tableswitch: {
duke@435 235 int lo = int_at(bci + 1 + pad + 4 * 1);
duke@435 236 int hi = int_at(bci + 1 + pad + 4 * 2);
duke@435 237 int n = hi - lo + 1;
duke@435 238 return 1 + pad + 4*(3 + n);
duke@435 239 }
duke@435 240 case Bytecodes::_lookupswitch:
duke@435 241 case Bytecodes::_fast_linearswitch:
duke@435 242 case Bytecodes::_fast_binaryswitch: {
duke@435 243 int npairs = int_at(bci + 1 + pad + 4 * 1);
duke@435 244 return 1 + pad + 4*(2 + 2*npairs);
duke@435 245 }
duke@435 246 default:
duke@435 247 ShouldNotReachHere();
duke@435 248 }
duke@435 249 }
duke@435 250 }
duke@435 251 return instruction_length_at(bci);
duke@435 252 }
duke@435 253
duke@435 254 // If a change item is recorded for "pc", with type "ct", returns the
duke@435 255 // associated padding, else -1.
duke@435 256 int Relocator::get_orig_switch_pad(int bci, bool is_lookup_switch) {
duke@435 257 for (int k = 0; k < _changes->length(); k++) {
duke@435 258 ChangeItem* ci = _changes->at(k);
duke@435 259 if (ci->is_switch_pad()) {
duke@435 260 ChangeSwitchPad* csp = (ChangeSwitchPad*)ci;
duke@435 261 if (csp->is_lookup_switch() == is_lookup_switch && csp->bci() == bci) {
duke@435 262 return csp->padding();
duke@435 263 }
duke@435 264 }
duke@435 265 }
duke@435 266 return -1;
duke@435 267 }
duke@435 268
duke@435 269
duke@435 270 // Push a ChangeJumpWiden if it doesn't already exist on the work queue,
duke@435 271 // otherwise adjust the item already there by delta. The calculation for
duke@435 272 // new_delta is wrong for this because it uses the offset stored in the
duke@435 273 // code stream itself which wasn't fixed when item was pushed on the work queue.
duke@435 274 void Relocator::push_jump_widen(int bci, int delta, int new_delta) {
duke@435 275 for (int j = 0; j < _changes->length(); j++) {
duke@435 276 ChangeItem* ci = _changes->at(j);
duke@435 277 if (ci->adjust(bci, delta)) return;
duke@435 278 }
duke@435 279 _changes->push(new ChangeJumpWiden(bci, new_delta));
duke@435 280 }
duke@435 281
duke@435 282
duke@435 283 // The current instruction of "c" is a jump; one of its offset starts
duke@435 284 // at "offset" and is a short if "isShort" is "TRUE",
duke@435 285 // and an integer otherwise. If the jump crosses "breakPC", change
duke@435 286 // the span of the jump by "delta".
duke@435 287 void Relocator::change_jump(int bci, int offset, bool is_short, int break_bci, int delta) {
duke@435 288 int bci_delta = (is_short) ? short_at(offset) : int_at(offset);
duke@435 289 int targ = bci + bci_delta;
duke@435 290
duke@435 291 if ((bci <= break_bci && targ > break_bci) ||
duke@435 292 (bci > break_bci && targ <= break_bci)) {
duke@435 293 int new_delta;
duke@435 294 if (bci_delta > 0)
duke@435 295 new_delta = bci_delta + delta;
duke@435 296 else
duke@435 297 new_delta = bci_delta - delta;
duke@435 298
duke@435 299 if (is_short && ((new_delta > MAX_SHORT) || new_delta < MIN_SHORT)) {
duke@435 300 push_jump_widen(bci, delta, new_delta);
duke@435 301 } else if (is_short) {
duke@435 302 short_at_put(offset, new_delta);
duke@435 303 } else {
duke@435 304 int_at_put(offset, new_delta);
duke@435 305 }
duke@435 306 }
duke@435 307 }
duke@435 308
duke@435 309
duke@435 310 // Changes all jumps crossing "break_bci" by "delta". May enqueue things
duke@435 311 // on "rc->changes"
duke@435 312 void Relocator::change_jumps(int break_bci, int delta) {
duke@435 313 int bci = 0;
duke@435 314 Bytecodes::Code bc;
duke@435 315 // Now, adjust any affected instructions.
duke@435 316 while (bci < code_length()) {
duke@435 317 switch (bc= code_at(bci)) {
duke@435 318 case Bytecodes::_ifeq:
duke@435 319 case Bytecodes::_ifne:
duke@435 320 case Bytecodes::_iflt:
duke@435 321 case Bytecodes::_ifge:
duke@435 322 case Bytecodes::_ifgt:
duke@435 323 case Bytecodes::_ifle:
duke@435 324 case Bytecodes::_if_icmpeq:
duke@435 325 case Bytecodes::_if_icmpne:
duke@435 326 case Bytecodes::_if_icmplt:
duke@435 327 case Bytecodes::_if_icmpge:
duke@435 328 case Bytecodes::_if_icmpgt:
duke@435 329 case Bytecodes::_if_icmple:
duke@435 330 case Bytecodes::_if_acmpeq:
duke@435 331 case Bytecodes::_if_acmpne:
duke@435 332 case Bytecodes::_ifnull:
duke@435 333 case Bytecodes::_ifnonnull:
duke@435 334 case Bytecodes::_goto:
duke@435 335 case Bytecodes::_jsr:
duke@435 336 change_jump(bci, bci+1, true, break_bci, delta);
duke@435 337 break;
duke@435 338 case Bytecodes::_goto_w:
duke@435 339 case Bytecodes::_jsr_w:
duke@435 340 change_jump(bci, bci+1, false, break_bci, delta);
duke@435 341 break;
duke@435 342 case Bytecodes::_tableswitch:
duke@435 343 case Bytecodes::_lookupswitch:
duke@435 344 case Bytecodes::_fast_linearswitch:
duke@435 345 case Bytecodes::_fast_binaryswitch: {
duke@435 346 int recPad = get_orig_switch_pad(bci, (bc != Bytecodes::_tableswitch));
duke@435 347 int oldPad = (recPad != -1) ? recPad : align(bci+1) - (bci+1);
duke@435 348 if (bci > break_bci) {
duke@435 349 int new_bci = bci + delta;
duke@435 350 int newPad = align(new_bci+1) - (new_bci+1);
duke@435 351 // Do we need to check the padding?
duke@435 352 if (newPad != oldPad) {
duke@435 353 if (recPad == -1) {
duke@435 354 _changes->push(new ChangeSwitchPad(bci, oldPad, (bc != Bytecodes::_tableswitch)));
duke@435 355 }
duke@435 356 }
duke@435 357 }
duke@435 358
duke@435 359 // Then the rest, which depend on the kind of switch.
duke@435 360 switch (bc) {
duke@435 361 case Bytecodes::_tableswitch: {
duke@435 362 change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
duke@435 363 // We cannot use the Bytecode_tableswitch abstraction, since the padding might not be correct.
duke@435 364 int lo = int_at(bci + 1 + oldPad + 4 * 1);
duke@435 365 int hi = int_at(bci + 1 + oldPad + 4 * 2);
duke@435 366 int n = hi - lo + 1;
duke@435 367 for (int k = 0; k < n; k++) {
duke@435 368 change_jump(bci, bci +1 + oldPad + 4*(k+3), false, break_bci, delta);
duke@435 369 }
duke@435 370 // Special next-bci calculation here...
duke@435 371 bci += 1 + oldPad + (n+3)*4;
duke@435 372 continue;
duke@435 373 }
duke@435 374 case Bytecodes::_lookupswitch:
duke@435 375 case Bytecodes::_fast_linearswitch:
duke@435 376 case Bytecodes::_fast_binaryswitch: {
duke@435 377 change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
duke@435 378 // We cannot use the Bytecode_lookupswitch abstraction, since the padding might not be correct.
duke@435 379 int npairs = int_at(bci + 1 + oldPad + 4 * 1);
duke@435 380 for (int k = 0; k < npairs; k++) {
duke@435 381 change_jump(bci, bci + 1 + oldPad + 4*(2 + 2*k + 1), false, break_bci, delta);
duke@435 382 }
duke@435 383 /* Special next-bci calculation here... */
duke@435 384 bci += 1 + oldPad + (2 + (npairs*2))*4;
duke@435 385 continue;
duke@435 386 }
duke@435 387 default:
duke@435 388 ShouldNotReachHere();
duke@435 389 }
duke@435 390 }
duke@435 391 default:
duke@435 392 break;
duke@435 393 }
duke@435 394 bci += rc_instr_len(bci);
duke@435 395 }
duke@435 396 }
duke@435 397
duke@435 398 // The width of instruction at "pc" is changing by "delta". Adjust the
duke@435 399 // exception table, if any, of "rc->mb".
duke@435 400 void Relocator::adjust_exception_table(int bci, int delta) {
jiangli@3917 401 ExceptionTable table(_method());
jiangli@3917 402 for (int index = 0; index < table.length(); index ++) {
jiangli@3917 403 if (table.start_pc(index) > bci) {
jiangli@3917 404 table.set_start_pc(index, table.start_pc(index) + delta);
jiangli@3917 405 table.set_end_pc(index, table.end_pc(index) + delta);
jiangli@3917 406 } else if (bci < table.end_pc(index)) {
jiangli@3917 407 table.set_end_pc(index, table.end_pc(index) + delta);
duke@435 408 }
jiangli@3917 409 if (table.handler_pc(index) > bci)
jiangli@3917 410 table.set_handler_pc(index, table.handler_pc(index) + delta);
duke@435 411 }
duke@435 412 }
duke@435 413
duke@435 414
duke@435 415 // The width of instruction at "bci" is changing by "delta". Adjust the line number table.
duke@435 416 void Relocator::adjust_line_no_table(int bci, int delta) {
duke@435 417 if (method()->has_linenumber_table()) {
duke@435 418 CompressedLineNumberReadStream reader(method()->compressed_linenumber_table());
duke@435 419 CompressedLineNumberWriteStream writer(64); // plenty big for most line number tables
duke@435 420 while (reader.read_pair()) {
duke@435 421 int adjustment = (reader.bci() > bci) ? delta : 0;
duke@435 422 writer.write_pair(reader.bci() + adjustment, reader.line());
duke@435 423 }
duke@435 424 writer.write_terminator();
duke@435 425 set_compressed_line_number_table(writer.buffer());
duke@435 426 set_compressed_line_number_table_size(writer.position());
duke@435 427 }
duke@435 428 }
duke@435 429
duke@435 430
duke@435 431 // The width of instruction at "bci" is changing by "delta". Adjust the local variable table.
duke@435 432 void Relocator::adjust_local_var_table(int bci, int delta) {
duke@435 433 int localvariable_table_length = method()->localvariable_table_length();
duke@435 434 if (localvariable_table_length > 0) {
duke@435 435 LocalVariableTableElement* table = method()->localvariable_table_start();
duke@435 436 for (int i = 0; i < localvariable_table_length; i++) {
duke@435 437 u2 current_bci = table[i].start_bci;
duke@435 438 if (current_bci > bci) {
duke@435 439 table[i].start_bci = current_bci + delta;
duke@435 440 } else {
duke@435 441 u2 current_length = table[i].length;
duke@435 442 if (current_bci + current_length > bci) {
duke@435 443 table[i].length = current_length + delta;
duke@435 444 }
duke@435 445 }
duke@435 446 }
duke@435 447 }
duke@435 448 }
duke@435 449
kamg@2232 450 // Create a new array, copying the src array but adding a hole at
kamg@2232 451 // the specified location
coleenp@4037 452 static Array<u1>* insert_hole_at(ClassLoaderData* loader_data,
coleenp@4037 453 size_t where, int hole_sz, Array<u1>* src) {
kamg@2232 454 Thread* THREAD = Thread::current();
coleenp@4037 455 Array<u1>* dst =
coleenp@4037 456 MetadataFactory::new_array<u1>(loader_data, src->length() + hole_sz, 0, CHECK_NULL);
kamg@2232 457
coleenp@4037 458 address src_addr = (address)src->adr_at(0);
coleenp@4037 459 address dst_addr = (address)dst->adr_at(0);
kamg@2232 460
kamg@2232 461 memcpy(dst_addr, src_addr, where);
kamg@2232 462 memcpy(dst_addr + where + hole_sz,
kamg@2232 463 src_addr + where, src->length() - where);
kamg@2232 464 return dst;
kamg@2232 465 }
kamg@2232 466
kamg@2232 467 // The width of instruction at "bci" is changing by "delta". Adjust the stack
kamg@2232 468 // map frames.
kamg@2232 469 void Relocator::adjust_stack_map_table(int bci, int delta) {
kamg@2232 470 if (method()->has_stackmap_table()) {
coleenp@4037 471 Array<u1>* data = method()->stackmap_data();
kamg@3992 472 // The data in the array is a classfile representation of the stackmap table
kamg@3992 473 stack_map_table* sm_table =
coleenp@4037 474 stack_map_table::at((address)data->adr_at(0));
kamg@2232 475
kamg@3992 476 int count = sm_table->number_of_entries();
kamg@3992 477 stack_map_frame* frame = sm_table->entries();
kamg@2232 478 int bci_iter = -1;
kamg@2232 479 bool offset_adjusted = false; // only need to adjust one offset
kamg@2232 480
kamg@2232 481 for (int i = 0; i < count; ++i) {
kamg@2232 482 int offset_delta = frame->offset_delta();
kamg@2232 483 bci_iter += offset_delta;
kamg@2232 484
kamg@2232 485 if (!offset_adjusted && bci_iter > bci) {
kamg@2232 486 int new_offset_delta = offset_delta + delta;
kamg@2232 487
kamg@2232 488 if (frame->is_valid_offset(new_offset_delta)) {
kamg@2232 489 frame->set_offset_delta(new_offset_delta);
kamg@2232 490 } else {
kamg@2232 491 assert(frame->is_same_frame() ||
kamg@3992 492 frame->is_same_locals_1_stack_item_frame(),
kamg@2232 493 "Frame must be one of the compressed forms");
kamg@2232 494 // The new delta exceeds the capacity of the 'same_frame' or
kamg@2232 495 // 'same_frame_1_stack_item_frame' frame types. We need to
kamg@2232 496 // convert these frames to the extended versions, but the extended
kamg@2232 497 // version is bigger and requires more room. So we allocate a
kamg@2232 498 // new array and copy the data, being sure to leave u2-sized hole
kamg@2232 499 // right after the 'frame_type' for the new offset field.
kamg@2232 500 //
kamg@2232 501 // We can safely ignore the reverse situation as a small delta
kamg@2232 502 // can still be used in an extended version of the frame.
kamg@2232 503
coleenp@4037 504 size_t frame_offset = (address)frame - (address)data->adr_at(0);
kamg@2232 505
coleenp@4037 506 ClassLoaderData* loader_data = method()->method_holder()->class_loader_data();
coleenp@4037 507 Array<u1>* new_data = insert_hole_at(loader_data, frame_offset + 1, 2, data);
coleenp@4037 508 if (new_data == NULL) {
kamg@2232 509 return; // out-of-memory?
kamg@2232 510 }
coleenp@4037 511 // Deallocate old data
coleenp@4037 512 MetadataFactory::free_array<u1>(loader_data, data);
coleenp@4037 513 data = new_data;
kamg@2232 514
coleenp@4037 515 address frame_addr = (address)(data->adr_at(0) + frame_offset);
kamg@2232 516 frame = stack_map_frame::at(frame_addr);
kamg@2232 517
kamg@2232 518
kamg@2232 519 // Now convert the frames in place
kamg@2232 520 if (frame->is_same_frame()) {
kamg@2232 521 same_frame_extended::create_at(frame_addr, new_offset_delta);
kamg@2232 522 } else {
kamg@3992 523 same_locals_1_stack_item_extended::create_at(
kamg@2232 524 frame_addr, new_offset_delta, NULL);
kamg@2232 525 // the verification_info_type should already be at the right spot
kamg@2232 526 }
kamg@2232 527 }
kamg@2232 528 offset_adjusted = true; // needs to be done only once, since subsequent
kamg@2232 529 // values are offsets from the current
kamg@2232 530 }
kamg@2232 531
kamg@2232 532 // The stack map frame may contain verification types, if so we need to
kamg@2232 533 // check and update any Uninitialized type's bci (no matter where it is).
kamg@2232 534 int number_of_types = frame->number_of_types();
kamg@2232 535 verification_type_info* types = frame->types();
kamg@2232 536
kamg@2232 537 for (int i = 0; i < number_of_types; ++i) {
kamg@2232 538 if (types->is_uninitialized() && types->bci() > bci) {
kamg@2232 539 types->set_bci(types->bci() + delta);
kamg@2232 540 }
kamg@2232 541 types = types->next();
kamg@2232 542 }
kamg@2232 543
kamg@2232 544 // Full frame has stack values too
kamg@2232 545 full_frame* ff = frame->as_full_frame();
kamg@2232 546 if (ff != NULL) {
kamg@2232 547 address eol = (address)types;
kamg@2232 548 number_of_types = ff->stack_slots(eol);
kamg@2232 549 types = ff->stack(eol);
kamg@2232 550 for (int i = 0; i < number_of_types; ++i) {
kamg@2232 551 if (types->is_uninitialized() && types->bci() > bci) {
kamg@2232 552 types->set_bci(types->bci() + delta);
kamg@2232 553 }
kamg@2232 554 types = types->next();
kamg@2232 555 }
kamg@2232 556 }
kamg@2232 557
kamg@2232 558 frame = frame->next();
kamg@2232 559 }
kamg@2232 560
kamg@2232 561 method()->set_stackmap_data(data); // in case it has changed
kamg@2232 562 }
kamg@2232 563 }
kamg@2232 564
duke@435 565
duke@435 566 bool Relocator::expand_code_array(int delta) {
duke@435 567 int length = MAX2(code_length() + delta, code_length() * (100+code_slop_pct()) / 100);
duke@435 568
duke@435 569 if (length > MAX_METHOD_LENGTH) {
duke@435 570 if (delta == 0 && code_length() <= MAX_METHOD_LENGTH) {
duke@435 571 length = MAX_METHOD_LENGTH;
duke@435 572 } else {
duke@435 573 return false;
duke@435 574 }
duke@435 575 }
duke@435 576
duke@435 577 unsigned char* new_code_array = NEW_RESOURCE_ARRAY(unsigned char, length);
duke@435 578 if (!new_code_array) return false;
duke@435 579
duke@435 580 // Expanding current array
duke@435 581 if (code_array() != NULL) {
duke@435 582 memcpy(new_code_array, code_array(), code_length());
duke@435 583 } else {
coleenp@4037 584 // Initial copy. Copy directly from Method*
duke@435 585 memcpy(new_code_array, method()->code_base(), code_length());
duke@435 586 }
duke@435 587
duke@435 588 set_code_array(new_code_array);
duke@435 589 set_code_array_length(length);
duke@435 590
duke@435 591 return true;
duke@435 592 }
duke@435 593
duke@435 594
duke@435 595 // The instruction at "bci", whose size is "ilen", is changing size by
duke@435 596 // "delta". Reallocate, move code, recalculate jumps, and enqueue
duke@435 597 // change items as necessary.
duke@435 598 bool Relocator::relocate_code(int bci, int ilen, int delta) {
duke@435 599 int next_bci = bci + ilen;
duke@435 600 if (delta > 0 && code_length() + delta > code_array_length()) {
duke@435 601 // Expand allocated code space, if necessary.
duke@435 602 if (!expand_code_array(delta)) {
duke@435 603 return false;
duke@435 604 }
duke@435 605 }
duke@435 606
duke@435 607 // We require 4-byte alignment of code arrays.
duke@435 608 assert(((intptr_t)code_array() & 3) == 0, "check code alignment");
duke@435 609 // Change jumps before doing the copying; this routine requires aligned switches.
duke@435 610 change_jumps(bci, delta);
duke@435 611
duke@435 612 // In case we have shrunken a tableswitch/lookupswitch statement, we store the last
duke@435 613 // bytes that get overwritten. We have to copy the bytes after the change_jumps method
duke@435 614 // has been called, since it is likly to update last offset in a tableswitch/lookupswitch
duke@435 615 if (delta < 0) {
duke@435 616 assert(delta>=-3, "we cannot overwrite more than 3 bytes");
duke@435 617 memcpy(_overwrite, addr_at(bci + ilen + delta), -delta);
duke@435 618 }
duke@435 619
duke@435 620 memmove(addr_at(next_bci + delta), addr_at(next_bci), code_length() - next_bci);
duke@435 621 set_code_length(code_length() + delta);
duke@435 622 // Also adjust exception tables...
duke@435 623 adjust_exception_table(bci, delta);
duke@435 624 // Line number tables...
duke@435 625 adjust_line_no_table(bci, delta);
duke@435 626 // And local variable table...
duke@435 627 adjust_local_var_table(bci, delta);
duke@435 628
kamg@2232 629 // Adjust stack maps
kamg@2232 630 adjust_stack_map_table(bci, delta);
kamg@2232 631
duke@435 632 // Relocate the pending change stack...
duke@435 633 for (int j = 0; j < _changes->length(); j++) {
duke@435 634 ChangeItem* ci = _changes->at(j);
duke@435 635 ci->relocate(bci, delta);
duke@435 636 }
duke@435 637
duke@435 638 // Notify any listeners about code relocation
duke@435 639 notify(bci, delta, code_length());
duke@435 640
duke@435 641 return true;
duke@435 642 }
duke@435 643
duke@435 644 // relocate a general instruction. Called by ChangeWiden class
duke@435 645 bool Relocator::handle_widen(int bci, int new_ilen, u_char inst_buffer[]) {
duke@435 646 int ilen = rc_instr_len(bci);
duke@435 647 if (!relocate_code(bci, ilen, new_ilen - ilen))
duke@435 648 return false;
duke@435 649
duke@435 650 // Insert new bytecode(s)
duke@435 651 for(int k = 0; k < new_ilen; k++) {
duke@435 652 code_at_put(bci + k, (Bytecodes::Code)inst_buffer[k]);
duke@435 653 }
duke@435 654
duke@435 655 return true;
duke@435 656 }
duke@435 657
duke@435 658 // handle jump_widen instruction. Called be ChangeJumpWiden class
duke@435 659 bool Relocator::handle_jump_widen(int bci, int delta) {
duke@435 660 int ilen = rc_instr_len(bci);
duke@435 661
duke@435 662 Bytecodes::Code bc = code_at(bci);
duke@435 663 switch (bc) {
duke@435 664 case Bytecodes::_ifeq:
duke@435 665 case Bytecodes::_ifne:
duke@435 666 case Bytecodes::_iflt:
duke@435 667 case Bytecodes::_ifge:
duke@435 668 case Bytecodes::_ifgt:
duke@435 669 case Bytecodes::_ifle:
duke@435 670 case Bytecodes::_if_icmpeq:
duke@435 671 case Bytecodes::_if_icmpne:
duke@435 672 case Bytecodes::_if_icmplt:
duke@435 673 case Bytecodes::_if_icmpge:
duke@435 674 case Bytecodes::_if_icmpgt:
duke@435 675 case Bytecodes::_if_icmple:
duke@435 676 case Bytecodes::_if_acmpeq:
duke@435 677 case Bytecodes::_if_acmpne:
duke@435 678 case Bytecodes::_ifnull:
duke@435 679 case Bytecodes::_ifnonnull: {
duke@435 680 const int goto_length = Bytecodes::length_for(Bytecodes::_goto);
duke@435 681
duke@435 682 // If 'if' points to the next bytecode after goto, it's already handled.
duke@435 683 // it shouldn't be.
duke@435 684 assert (short_at(bci+1) != ilen+goto_length, "if relocation already handled");
duke@435 685 assert(ilen == 3, "check length");
duke@435 686
duke@435 687 // Convert to 0 if <cond> goto 6
duke@435 688 // 3 _goto 11
duke@435 689 // 6 _goto_w <wide delta offset>
duke@435 690 // 11 <else code>
duke@435 691 const int goto_w_length = Bytecodes::length_for(Bytecodes::_goto_w);
duke@435 692 const int add_bci = goto_length + goto_w_length;
duke@435 693
duke@435 694 if (!relocate_code(bci, 3, /*delta*/add_bci)) return false;
duke@435 695
duke@435 696 // if bytecode points to goto_w instruction
duke@435 697 short_at_put(bci + 1, ilen + goto_length);
duke@435 698
duke@435 699 int cbci = bci + ilen;
duke@435 700 // goto around
duke@435 701 code_at_put(cbci, Bytecodes::_goto);
duke@435 702 short_at_put(cbci + 1, add_bci);
duke@435 703 // goto_w <wide delta>
duke@435 704 cbci = cbci + goto_length;
duke@435 705 code_at_put(cbci, Bytecodes::_goto_w);
duke@435 706 if (delta > 0) {
duke@435 707 delta += 2; // goto_w is 2 bytes more than "if" code
duke@435 708 } else {
duke@435 709 delta -= ilen+goto_length; // branch starts at goto_w offset
duke@435 710 }
duke@435 711 int_at_put(cbci + 1, delta);
duke@435 712 break;
duke@435 713
duke@435 714 }
duke@435 715 case Bytecodes::_goto:
duke@435 716 case Bytecodes::_jsr:
duke@435 717 assert(ilen == 3, "check length");
duke@435 718
duke@435 719 if (!relocate_code(bci, 3, 2)) return false;
duke@435 720 if (bc == Bytecodes::_goto)
duke@435 721 code_at_put(bci, Bytecodes::_goto_w);
duke@435 722 else
duke@435 723 code_at_put(bci, Bytecodes::_jsr_w);
duke@435 724
duke@435 725 // If it's a forward jump, add 2 for the widening.
duke@435 726 if (delta > 0) delta += 2;
duke@435 727 int_at_put(bci + 1, delta);
duke@435 728 break;
duke@435 729
duke@435 730 default: ShouldNotReachHere();
duke@435 731 }
duke@435 732
duke@435 733 return true;
duke@435 734 }
duke@435 735
duke@435 736 // handle lookup/table switch instructions. Called be ChangeSwitchPad class
duke@435 737 bool Relocator::handle_switch_pad(int bci, int old_pad, bool is_lookup_switch) {
duke@435 738 int ilen = rc_instr_len(bci);
duke@435 739 int new_pad = align(bci+1) - (bci+1);
duke@435 740 int pad_delta = new_pad - old_pad;
duke@435 741 if (pad_delta != 0) {
duke@435 742 int len;
duke@435 743 if (!is_lookup_switch) {
duke@435 744 int low = int_at(bci+1+old_pad+4);
duke@435 745 int high = int_at(bci+1+old_pad+8);
duke@435 746 len = high-low+1 + 3; // 3 for default, hi, lo.
duke@435 747 } else {
duke@435 748 int npairs = int_at(bci+1+old_pad+4);
duke@435 749 len = npairs*2 + 2; // 2 for default, npairs.
duke@435 750 }
duke@435 751 // Because "relocateCode" does a "changeJumps" loop,
duke@435 752 // which parses instructions to determine their length,
duke@435 753 // we need to call that before messing with the current
duke@435 754 // instruction. Since it may also overwrite the current
duke@435 755 // instruction when moving down, remember the possibly
duke@435 756 // overwritten part.
duke@435 757
duke@435 758 // Move the code following the instruction...
duke@435 759 if (!relocate_code(bci, ilen, pad_delta)) return false;
duke@435 760
duke@435 761 if (pad_delta < 0) {
duke@435 762 // Move the shrunken instruction down.
duke@435 763 memmove(addr_at(bci + 1 + new_pad),
duke@435 764 addr_at(bci + 1 + old_pad),
duke@435 765 len * 4 + pad_delta);
duke@435 766 memmove(addr_at(bci + 1 + new_pad + len*4 + pad_delta),
duke@435 767 _overwrite, -pad_delta);
duke@435 768 } else {
duke@435 769 assert(pad_delta > 0, "check");
duke@435 770 // Move the expanded instruction up.
duke@435 771 memmove(addr_at(bci +1 + new_pad),
duke@435 772 addr_at(bci +1 + old_pad),
duke@435 773 len * 4);
kamg@2232 774 memset(addr_at(bci + 1), 0, new_pad); // pad must be 0
duke@435 775 }
duke@435 776 }
duke@435 777 return true;
duke@435 778 }

mercurial