src/share/vm/runtime/relocator.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

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

mercurial