jdk

Форк
0
/
relocator.cpp 
793 строки · 27.3 Кб
1
/*
2
 * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 *
23
 */
24

25
#include "precompiled.hpp"
26
#include "classfile/stackMapTableFormat.hpp"
27
#include "interpreter/bytecodes.hpp"
28
#include "memory/metadataFactory.hpp"
29
#include "memory/oopFactory.hpp"
30
#include "oops/method.inline.hpp"
31
#include "oops/oop.inline.hpp"
32
#include "runtime/handles.inline.hpp"
33
#include "runtime/relocator.hpp"
34
#include "utilities/checkedCast.hpp"
35

36
#define MAX_METHOD_LENGTH  65535
37

38
#define MAX_SHORT ((1 << 15) - 1)
39
#define MIN_SHORT (- (1 << 15))
40

41
// Encapsulates a code change request. There are 3 types.
42
// General instruction, jump instruction, and table/lookup switches
43
//
44
class ChangeItem : public ResourceObj {
45
  int _bci;
46
 public:
47
   ChangeItem(int bci) { _bci = bci; }
48
   virtual bool handle_code_change(Relocator *r) = 0;
49

50
   // type info
51
   virtual bool is_switch_pad() { return false; }
52

53
   // accessors
54
   int bci()    { return _bci; }
55
   void relocate(int break_bci, int delta) { if (_bci > break_bci) { _bci += delta; } }
56

57
   virtual bool adjust(int bci, int delta) { return false; }
58

59
   // debug
60
   virtual void print() = 0;
61
};
62

63
class ChangeWiden : public ChangeItem {
64
  int              _new_ilen;    // New length of instruction at bci
65
  u_char*          _inst_buffer; // New bytecodes
66
 public:
67
  ChangeWiden(int bci, int new_ilen, u_char* inst_buffer) : ChangeItem(bci) {
68
    _new_ilen = new_ilen;
69
    _inst_buffer = inst_buffer;
70
  }
71

72
  // Callback to do instruction
73
  bool handle_code_change(Relocator *r) { return r->handle_widen(bci(), _new_ilen, _inst_buffer); };
74

75
  void print()                 { tty->print_cr("ChangeWiden. bci: %d   New_ilen: %d", bci(), _new_ilen); }
76
};
77

78
class ChangeJumpWiden : public ChangeItem {
79
  int _delta;  // New length of instruction at bci
80
 public:
81
  ChangeJumpWiden(int bci, int delta) : ChangeItem(bci) { _delta = delta; }
82

83
  // Callback to do instruction
84
  bool handle_code_change(Relocator *r) { return r->handle_jump_widen(bci(), _delta); };
85

86
  // If the bci matches, adjust the delta in the change jump request.
87
  bool adjust(int jump_bci, int delta) {
88
    if (bci() == jump_bci) {
89
      if (_delta > 0)
90
        _delta += delta;
91
      else
92
        _delta -= delta;
93
      return true;
94
    }
95
    return false;
96
  }
97

98
  void print()                 { tty->print_cr("ChangeJumpWiden. bci: %d   Delta: %d", bci(), _delta); }
99
};
100

101
class ChangeSwitchPad : public ChangeItem {
102
  int  _padding;
103
  bool _is_lookup_switch;
104
 public:
105
   ChangeSwitchPad(int bci, int padding, bool is_lookup_switch) : ChangeItem(bci) {
106
     _padding = padding;
107
     _is_lookup_switch = is_lookup_switch;
108
   }
109

110
   // Callback to do instruction
111
   bool handle_code_change(Relocator *r) { return r->handle_switch_pad(bci(), _padding, _is_lookup_switch); };
112

113
   bool is_switch_pad()        { return true; }
114
   int  padding()              { return _padding;  }
115
   bool is_lookup_switch()     { return _is_lookup_switch; }
116

117
   void print()                { tty->print_cr("ChangeSwitchPad. bci: %d   Padding: %d  IsLookupSwitch: %d", bci(), _padding, _is_lookup_switch); }
118
};
119

120
//-----------------------------------------------------------------------------------------------------------
121
// Relocator code
122

123
Relocator::Relocator(const methodHandle& m, RelocatorListener* listener) {
124
  set_method(m);
125
  set_code_length(method()->code_size());
126
  set_code_array(nullptr);
127
  // Allocate code array and copy bytecodes
128
  if (!expand_code_array(0)) {
129
    // Should have at least MAX_METHOD_LENGTH available or the verifier
130
    // would have failed.
131
    ShouldNotReachHere();
132
  }
133
  set_compressed_line_number_table(nullptr);
134
  set_compressed_line_number_table_size(0);
135
  _listener = listener;
136
}
137

138
// size is the new size of the instruction at bci. Hence, if size is less than the current
139
// instruction size, we will shrink the code.
140
methodHandle Relocator::insert_space_at(int bci, int size, u_char inst_buffer[], TRAPS) {
141
  _changes = new GrowableArray<ChangeItem*> (10);
142
  _changes->push(new ChangeWiden(bci, size, inst_buffer));
143

144
  if (TraceRelocator) {
145
    tty->print_cr("Space at: %d Size: %d", bci, size);
146
    _method->print();
147
    _method->print_codes();
148
    tty->print_cr("-------------------------------------------------");
149
  }
150

151
  if (!handle_code_changes()) return methodHandle();
152

153
    // Construct the new method
154
  methodHandle new_method = Method::clone_with_new_data(method(),
155
                              code_array(), code_length(),
156
                              compressed_line_number_table(),
157
                              compressed_line_number_table_size(),
158
                              CHECK_(methodHandle()));
159

160
  // Deallocate the old Method* from metadata
161
  ClassLoaderData* loader_data = method()->method_holder()->class_loader_data();
162
  loader_data->add_to_deallocate_list(method()());
163

164
    set_method(new_method);
165

166
  if (TraceRelocator) {
167
    tty->print_cr("-------------------------------------------------");
168
    tty->print_cr("new method");
169
    _method->print_codes();
170
  }
171

172
  return new_method;
173
}
174

175

176
bool Relocator::handle_code_changes() {
177
  assert(_changes != nullptr, "changes vector must be initialized");
178

179
  while (!_changes->is_empty()) {
180
    // Inv: everything is aligned.
181
    ChangeItem* ci = _changes->first();
182

183
    if (TraceRelocator) {
184
      ci->print();
185
    }
186

187
    // Execute operation
188
    if (!ci->handle_code_change(this)) return false;
189

190
    // Shuffle items up
191
    for (int index = 1; index < _changes->length(); index++) {
192
      _changes->at_put(index-1, _changes->at(index));
193
    }
194
    _changes->pop();
195
  }
196
  return true;
197
}
198

199

200
bool Relocator::is_opcode_lookupswitch(Bytecodes::Code bc) {
201
  switch (bc) {
202
    case Bytecodes::_tableswitch:       return false;
203
    case Bytecodes::_lookupswitch:                   // not rewritten on ia64
204
    case Bytecodes::_fast_linearswitch:              // rewritten _lookupswitch
205
    case Bytecodes::_fast_binaryswitch: return true; // rewritten _lookupswitch
206
    default: ShouldNotReachHere();
207
  }
208
  return true; // dummy
209
}
210

211
// We need a special instruction size method, since lookupswitches and tableswitches might not be
212
// properly aligned during relocation
213
int Relocator::rc_instr_len(int bci) {
214
  Bytecodes::Code bc= code_at(bci);
215
  switch (bc) {
216
    // In the case of switch instructions, see if we have the original
217
    // padding recorded.
218
    case Bytecodes::_tableswitch:
219
    case Bytecodes::_lookupswitch:
220
    case Bytecodes::_fast_linearswitch:
221
    case Bytecodes::_fast_binaryswitch:
222
    {
223
      int pad = get_orig_switch_pad(bci, is_opcode_lookupswitch(bc));
224
      if (pad == -1) {
225
        return instruction_length_at(bci);
226
      }
227
      // Otherwise, depends on the switch type.
228
      switch (bc) {
229
        case Bytecodes::_tableswitch: {
230
          int lo = int_at(bci + 1 + pad + 4 * 1);
231
          int hi = int_at(bci + 1 + pad + 4 * 2);
232
          int n = hi - lo + 1;
233
          return 1 + pad + 4*(3 + n);
234
        }
235
        case Bytecodes::_lookupswitch:
236
        case Bytecodes::_fast_linearswitch:
237
        case Bytecodes::_fast_binaryswitch: {
238
          int npairs = int_at(bci + 1 + pad + 4 * 1);
239
          return 1 + pad + 4*(2 + 2*npairs);
240
        }
241
        default:
242
          ShouldNotReachHere();
243
      }
244
    }
245
    default:
246
      break;
247
  }
248
  return instruction_length_at(bci);
249
}
250

251
// If a change item is recorded for "pc", with type "ct", returns the
252
// associated padding, else -1.
253
int Relocator::get_orig_switch_pad(int bci, bool is_lookup_switch) {
254
  for (int k = 0; k < _changes->length(); k++) {
255
    ChangeItem* ci = _changes->at(k);
256
    if (ci->is_switch_pad()) {
257
      ChangeSwitchPad* csp = (ChangeSwitchPad*)ci;
258
      if (csp->is_lookup_switch() == is_lookup_switch && csp->bci() == bci) {
259
        return csp->padding();
260
      }
261
    }
262
  }
263
  return -1;
264
}
265

266

267
// Push a ChangeJumpWiden if it doesn't already exist on the work queue,
268
// otherwise adjust the item already there by delta.  The calculation for
269
// new_delta is wrong for this because it uses the offset stored in the
270
// code stream itself which wasn't fixed when item was pushed on the work queue.
271
void Relocator::push_jump_widen(int bci, int delta, int new_delta) {
272
  for (int j = 0; j < _changes->length(); j++) {
273
    ChangeItem* ci = _changes->at(j);
274
    if (ci->adjust(bci, delta)) return;
275
  }
276
  _changes->push(new ChangeJumpWiden(bci, new_delta));
277
}
278

279

280
// The current instruction of "c" is a jump; one of its offset starts
281
// at "offset" and is a short if "isShort" is "TRUE",
282
// and an integer otherwise.  If the jump crosses "breakPC", change
283
// the span of the jump by "delta".
284
void Relocator::change_jump(int bci, int offset, bool is_short, int break_bci, int delta) {
285
  int bci_delta = (is_short) ? short_at(offset) : int_at(offset);
286
  int targ = bci + bci_delta;
287

288
  if ((bci <= break_bci && targ >  break_bci) ||
289
      (bci >  break_bci && targ <= break_bci)) {
290
    int new_delta;
291
    if (bci_delta > 0)
292
      new_delta = bci_delta + delta;
293
    else
294
      new_delta = bci_delta - delta;
295

296
    if (is_short && ((new_delta > MAX_SHORT) || new_delta < MIN_SHORT)) {
297
      push_jump_widen(bci, delta, new_delta);
298
    } else if (is_short) {
299
      short_at_put(offset, checked_cast<short>(new_delta));
300
    } else {
301
      int_at_put(offset, new_delta);
302
    }
303
  }
304
}
305

306

307
// Changes all jumps crossing "break_bci" by "delta".  May enqueue things
308
// on "rc->changes"
309
void Relocator::change_jumps(int break_bci, int delta) {
310
  int bci = 0;
311
  Bytecodes::Code bc;
312
  // Now, adjust any affected instructions.
313
  while (bci < code_length()) {
314
    switch (bc= code_at(bci)) {
315
      case Bytecodes::_ifeq:
316
      case Bytecodes::_ifne:
317
      case Bytecodes::_iflt:
318
      case Bytecodes::_ifge:
319
      case Bytecodes::_ifgt:
320
      case Bytecodes::_ifle:
321
      case Bytecodes::_if_icmpeq:
322
      case Bytecodes::_if_icmpne:
323
      case Bytecodes::_if_icmplt:
324
      case Bytecodes::_if_icmpge:
325
      case Bytecodes::_if_icmpgt:
326
      case Bytecodes::_if_icmple:
327
      case Bytecodes::_if_acmpeq:
328
      case Bytecodes::_if_acmpne:
329
      case Bytecodes::_ifnull:
330
      case Bytecodes::_ifnonnull:
331
      case Bytecodes::_goto:
332
      case Bytecodes::_jsr:
333
        change_jump(bci, bci+1, true, break_bci, delta);
334
        break;
335
      case Bytecodes::_goto_w:
336
      case Bytecodes::_jsr_w:
337
        change_jump(bci, bci+1, false, break_bci, delta);
338
        break;
339
      case Bytecodes::_tableswitch:
340
      case Bytecodes::_lookupswitch:
341
      case Bytecodes::_fast_linearswitch:
342
      case Bytecodes::_fast_binaryswitch: {
343
        int recPad = get_orig_switch_pad(bci, (bc != Bytecodes::_tableswitch));
344
        int oldPad = (recPad != -1) ? recPad : align(bci+1) - (bci+1);
345
        if (bci > break_bci) {
346
          int new_bci = bci + delta;
347
          int newPad = align(new_bci+1) - (new_bci+1);
348
          // Do we need to check the padding?
349
          if (newPad != oldPad) {
350
            if (recPad == -1) {
351
              _changes->push(new ChangeSwitchPad(bci, oldPad, (bc != Bytecodes::_tableswitch)));
352
            }
353
          }
354
        }
355

356
        // Then the rest, which depend on the kind of switch.
357
        switch (bc) {
358
          case Bytecodes::_tableswitch: {
359
            change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
360
            // We cannot use the Bytecode_tableswitch abstraction, since the padding might not be correct.
361
            int lo = int_at(bci + 1 + oldPad + 4 * 1);
362
            int hi = int_at(bci + 1 + oldPad + 4 * 2);
363
            int n = hi - lo + 1;
364
            for (int k = 0; k < n; k++) {
365
              change_jump(bci, bci +1 + oldPad + 4*(k+3), false, break_bci, delta);
366
            }
367
            // Special next-bci calculation here...
368
            bci += 1 + oldPad + (n+3)*4;
369
            continue;
370
          }
371
          case Bytecodes::_lookupswitch:
372
          case Bytecodes::_fast_linearswitch:
373
          case Bytecodes::_fast_binaryswitch: {
374
            change_jump(bci, bci +1 + oldPad, false, break_bci, delta);
375
            // We cannot use the Bytecode_lookupswitch abstraction, since the padding might not be correct.
376
            int npairs = int_at(bci + 1 + oldPad + 4 * 1);
377
            for (int k = 0; k < npairs; k++) {
378
              change_jump(bci, bci + 1 + oldPad + 4*(2 + 2*k + 1), false, break_bci, delta);
379
            }
380
            /* Special next-bci calculation here... */
381
            bci += 1 + oldPad + (2 + (npairs*2))*4;
382
            continue;
383
          }
384
          default:
385
            ShouldNotReachHere();
386
        }
387
      }
388
      default:
389
        break;
390
    }
391
    bci += rc_instr_len(bci);
392
  }
393
}
394

395
// The width of instruction at "pc" is changing by "delta".  Adjust the
396
// exception table, if any, of "rc->mb".
397
void Relocator::adjust_exception_table(int bci, int delta) {
398
  ExceptionTable table(_method());
399
  for (int index = 0; index < table.length(); index ++) {
400
    if (table.start_pc(index) > bci) {
401
      table.set_start_pc(index, checked_cast<u2>(table.start_pc(index) + delta));
402
      table.set_end_pc(index, checked_cast<u2>(table.end_pc(index) + delta));
403
    } else if (bci < table.end_pc(index)) {
404
      table.set_end_pc(index, checked_cast<u2>(table.end_pc(index) + delta));
405
    }
406
    if (table.handler_pc(index) > bci)
407
      table.set_handler_pc(index, checked_cast<u2>(table.handler_pc(index) + delta));
408
  }
409
}
410

411
static void print_linenumber_table(unsigned char* table) {
412
  CompressedLineNumberReadStream stream(table);
413
  tty->print_cr("-------------------------------------------------");
414
  while (stream.read_pair()) {
415
    tty->print_cr("   - line %d: %d", stream.line(), stream.bci());
416
  }
417
  tty->print_cr("-------------------------------------------------");
418
}
419

420
// The width of instruction at "bci" is changing by "delta".  Adjust the line number table.
421
void Relocator::adjust_line_no_table(int bci, int delta) {
422
  if (method()->has_linenumber_table()) {
423
    // if we already made adjustments then use the updated table
424
    unsigned char *table = compressed_line_number_table();
425
    if (table == nullptr) {
426
      table = method()->compressed_linenumber_table();
427
    }
428
    CompressedLineNumberReadStream  reader(table);
429
    CompressedLineNumberWriteStream writer(64);  // plenty big for most line number tables
430
    while (reader.read_pair()) {
431
      int adjustment = (reader.bci() > bci) ? delta : 0;
432
      writer.write_pair(reader.bci() + adjustment, reader.line());
433
    }
434
    writer.write_terminator();
435
    set_compressed_line_number_table(writer.buffer());
436
    set_compressed_line_number_table_size(writer.position());
437
    if (TraceRelocator) {
438
      tty->print_cr("Adjusted line number table");
439
      print_linenumber_table(compressed_line_number_table());
440
    }
441
  }
442
}
443

444

445
// The width of instruction at "bci" is changing by "delta".  Adjust the local variable table.
446
void Relocator::adjust_local_var_table(int bci, int delta) {
447
  int localvariable_table_length = method()->localvariable_table_length();
448
  if (localvariable_table_length > 0) {
449
    LocalVariableTableElement* table = method()->localvariable_table_start();
450
    for (int i = 0; i < localvariable_table_length; i++) {
451
      u2 current_bci = table[i].start_bci;
452
      if (current_bci > bci) {
453
        table[i].start_bci = checked_cast<u2>(current_bci + delta);
454
      } else {
455
        u2 current_length = table[i].length;
456
        if (current_bci + current_length > bci) {
457
          table[i].length = checked_cast<u2>(current_length + delta);
458
        }
459
      }
460
    }
461
  }
462
}
463

464
// Create a new array, copying the src array but adding a hole at
465
// the specified location
466
static Array<u1>* insert_hole_at(ClassLoaderData* loader_data,
467
    size_t where, int hole_sz, Array<u1>* src) {
468
  JavaThread* THREAD = JavaThread::current(); // For exception macros.
469
  Array<u1>* dst =
470
      MetadataFactory::new_array<u1>(loader_data, src->length() + hole_sz, 0, CHECK_NULL);
471

472
  address src_addr = (address)src->adr_at(0);
473
  address dst_addr = (address)dst->adr_at(0);
474

475
  memcpy(dst_addr, src_addr, where);
476
  memcpy(dst_addr + where + hole_sz,
477
         src_addr + where, src->length() - where);
478
  return dst;
479
}
480

481
// The width of instruction at "bci" is changing by "delta".  Adjust the stack
482
// map frames.
483
void Relocator::adjust_stack_map_table(int bci, int delta) {
484
  if (method()->has_stackmap_table()) {
485
    Array<u1>* data = method()->stackmap_data();
486
    // The data in the array is a classfile representation of the stackmap table
487
    stack_map_table* sm_table =
488
        stack_map_table::at((address)data->adr_at(0));
489

490
    int count = sm_table->number_of_entries();
491
    stack_map_frame* frame = sm_table->entries();
492
    int bci_iter = -1;
493
    bool offset_adjusted = false; // only need to adjust one offset
494

495
    for (int i = 0; i < count; ++i) {
496
      int offset_delta = frame->offset_delta();
497
      bci_iter += offset_delta;
498

499
      if (!offset_adjusted && bci_iter > bci) {
500
        int new_offset_delta = offset_delta + delta;
501

502
        if (frame->is_valid_offset(new_offset_delta)) {
503
          frame->set_offset_delta(new_offset_delta);
504
        } else {
505
          assert(frame->is_same_frame() ||
506
                 frame->is_same_locals_1_stack_item_frame(),
507
                 "Frame must be one of the compressed forms");
508
          // The new delta exceeds the capacity of the 'same_frame' or
509
          // 'same_frame_1_stack_item_frame' frame types.  We need to
510
          // convert these frames to the extended versions, but the extended
511
          // version is bigger and requires more room.  So we allocate a
512
          // new array and copy the data, being sure to leave u2-sized hole
513
          // right after the 'frame_type' for the new offset field.
514
          //
515
          // We can safely ignore the reverse situation as a small delta
516
          // can still be used in an extended version of the frame.
517

518
          size_t frame_offset = (address)frame - (address)data->adr_at(0);
519

520
          ClassLoaderData* loader_data = method()->method_holder()->class_loader_data();
521
          Array<u1>* new_data = insert_hole_at(loader_data, frame_offset + 1, 2, data);
522
          if (new_data == nullptr) {
523
            return; // out-of-memory?
524
          }
525
          // Deallocate old data
526
          MetadataFactory::free_array<u1>(loader_data, data);
527
          data = new_data;
528

529
          address frame_addr = (address)(data->adr_at(0) + frame_offset);
530
          frame = stack_map_frame::at(frame_addr);
531

532

533
          // Now convert the frames in place
534
          if (frame->is_same_frame()) {
535
            same_frame_extended::create_at(frame_addr, checked_cast<u2>(new_offset_delta));
536
          } else {
537
            same_locals_1_stack_item_extended::create_at(
538
              frame_addr, new_offset_delta, nullptr);
539
            // the verification_info_type should already be at the right spot
540
          }
541
        }
542
        offset_adjusted = true; // needs to be done only once, since subsequent
543
                                // values are offsets from the current
544
      }
545

546
      // The stack map frame may contain verification types, if so we need to
547
      // check and update any Uninitialized type's bci (no matter where it is).
548
      int number_of_types = frame->number_of_types();
549
      verification_type_info* types = frame->types();
550

551
      for (int i = 0; i < number_of_types; ++i) {
552
        if (types->is_uninitialized() && types->bci() > bci) {
553
          types->set_bci(checked_cast<u2>(types->bci() + delta));
554
        }
555
        types = types->next();
556
      }
557

558
      // Full frame has stack values too
559
      full_frame* ff = frame->as_full_frame();
560
      if (ff != nullptr) {
561
        address eol = (address)types;
562
        number_of_types = ff->stack_slots(eol);
563
        types = ff->stack(eol);
564
        for (int i = 0; i < number_of_types; ++i) {
565
          if (types->is_uninitialized() && types->bci() > bci) {
566
            types->set_bci(checked_cast<u2>(types->bci() + delta));
567
          }
568
          types = types->next();
569
        }
570
      }
571

572
      frame = frame->next();
573
    }
574

575
    method()->set_stackmap_data(data); // in case it has changed
576
  }
577
}
578

579

580
bool Relocator::expand_code_array(int delta) {
581
  int length = MAX2(code_length() + delta, code_length() * (100+code_slop_pct()) / 100);
582

583
  if (length > MAX_METHOD_LENGTH) {
584
    if (delta == 0 && code_length() <= MAX_METHOD_LENGTH) {
585
      length = MAX_METHOD_LENGTH;
586
    } else {
587
      return false;
588
    }
589
  }
590

591
  unsigned char* new_code_array = NEW_RESOURCE_ARRAY(unsigned char, length);
592
  if (!new_code_array) return false;
593

594
  // Expanding current array
595
  if (code_array() != nullptr) {
596
    memcpy(new_code_array, code_array(), code_length());
597
  } else {
598
    // Initial copy. Copy directly from Method*
599
    memcpy(new_code_array, method()->code_base(), code_length());
600
  }
601

602
  set_code_array(new_code_array);
603
  set_code_array_length(length);
604

605
  return true;
606
}
607

608

609
// The instruction at "bci", whose size is "ilen", is changing size by
610
// "delta".  Reallocate, move code, recalculate jumps, and enqueue
611
// change items as necessary.
612
bool Relocator::relocate_code(int bci, int ilen, int delta) {
613
  int next_bci = bci + ilen;
614
  if (delta > 0 && code_length() + delta > code_array_length())  {
615
    // Expand allocated code space, if necessary.
616
    if (!expand_code_array(delta)) {
617
          return false;
618
    }
619
  }
620

621
  // We require 4-byte alignment of code arrays.
622
  assert(((intptr_t)code_array() & 3) == 0, "check code alignment");
623
  // Change jumps before doing the copying; this routine requires aligned switches.
624
  change_jumps(bci, delta);
625

626
  // In case we have shrunken a tableswitch/lookupswitch statement, we store the last
627
  // bytes that get overwritten. We have to copy the bytes after the change_jumps method
628
  // has been called, since it is likely to update last offset in a tableswitch/lookupswitch
629
  assert(delta >= -3, "We cannot overwrite more than 3 bytes.");
630
  if (delta < 0 && delta >= -3) {
631
    memcpy(_overwrite, addr_at(bci + ilen + delta), -delta);
632
  }
633

634
  memmove(addr_at(next_bci + delta), addr_at(next_bci), code_length() - next_bci);
635
  set_code_length(code_length() + delta);
636

637
  // Also adjust exception tables...
638
  adjust_exception_table(bci, delta);
639
  // Line number tables...
640
  adjust_line_no_table(bci, delta);
641
  // And local variable table...
642
  adjust_local_var_table(bci, delta);
643

644
  // Adjust stack maps
645
  adjust_stack_map_table(bci, delta);
646

647
  // Relocate the pending change stack...
648
  for (int j = 0; j < _changes->length(); j++) {
649
    ChangeItem* ci = _changes->at(j);
650
    ci->relocate(bci, delta);
651
  }
652

653
  // Notify any listeners about code relocation
654
  notify(bci, delta, code_length());
655

656
  return true;
657
}
658

659
// relocate a general instruction. Called by ChangeWiden class
660
bool Relocator::handle_widen(int bci, int new_ilen, u_char inst_buffer[]) {
661
  int ilen = rc_instr_len(bci);
662
  if (!relocate_code(bci, ilen, new_ilen - ilen))
663
    return false;
664

665
  // Insert new bytecode(s)
666
  for(int k = 0; k < new_ilen; k++) {
667
    code_at_put(bci + k, (Bytecodes::Code)inst_buffer[k]);
668
  }
669

670
  return true;
671
}
672

673
// handle jump_widen instruction. Called be ChangeJumpWiden class
674
bool Relocator::handle_jump_widen(int bci, int delta) {
675
  int ilen = rc_instr_len(bci);
676

677
  Bytecodes::Code bc = code_at(bci);
678
  switch (bc) {
679
    case Bytecodes::_ifeq:
680
    case Bytecodes::_ifne:
681
    case Bytecodes::_iflt:
682
    case Bytecodes::_ifge:
683
    case Bytecodes::_ifgt:
684
    case Bytecodes::_ifle:
685
    case Bytecodes::_if_icmpeq:
686
    case Bytecodes::_if_icmpne:
687
    case Bytecodes::_if_icmplt:
688
    case Bytecodes::_if_icmpge:
689
    case Bytecodes::_if_icmpgt:
690
    case Bytecodes::_if_icmple:
691
    case Bytecodes::_if_acmpeq:
692
    case Bytecodes::_if_acmpne:
693
    case Bytecodes::_ifnull:
694
    case Bytecodes::_ifnonnull: {
695
      const int goto_length   = Bytecodes::length_for(Bytecodes::_goto);
696

697
      // If 'if' points to the next bytecode after goto, it's already handled.
698
      // it shouldn't be.
699
      assert (short_at(bci+1) != ilen+goto_length, "if relocation already handled");
700
      assert(ilen == 3, "check length");
701

702
      // Convert to 0 if <cond> goto 6
703
      //            3 _goto 11
704
      //            6 _goto_w <wide delta offset>
705
      //            11 <else code>
706
      const int goto_w_length = Bytecodes::length_for(Bytecodes::_goto_w);
707
      const int add_bci = goto_length + goto_w_length;
708

709
      if (!relocate_code(bci, 3, /*delta*/add_bci)) return false;
710

711
      // if bytecode points to goto_w instruction
712
      short_at_put(bci + 1, checked_cast<short>(ilen + goto_length));
713

714
      int cbci = bci + ilen;
715
      // goto around
716
      code_at_put(cbci, Bytecodes::_goto);
717
      short_at_put(cbci + 1, checked_cast<short>(add_bci));
718
      // goto_w <wide delta>
719
      cbci = cbci + goto_length;
720
      code_at_put(cbci, Bytecodes::_goto_w);
721
      if (delta > 0) {
722
        delta += 2;                 // goto_w is 2 bytes more than "if" code
723
      } else {
724
        delta -= ilen+goto_length;  // branch starts at goto_w offset
725
      }
726
      int_at_put(cbci + 1, delta);
727
      break;
728

729
      }
730
    case Bytecodes::_goto:
731
    case Bytecodes::_jsr:
732
      assert(ilen == 3, "check length");
733

734
      if (!relocate_code(bci, 3, 2)) return false;
735
      if (bc == Bytecodes::_goto)
736
        code_at_put(bci, Bytecodes::_goto_w);
737
      else
738
        code_at_put(bci, Bytecodes::_jsr_w);
739

740
      // If it's a forward jump, add 2 for the widening.
741
      if (delta > 0) delta += 2;
742
      int_at_put(bci + 1, delta);
743
      break;
744

745
    default: ShouldNotReachHere();
746
  }
747

748
  return true;
749
}
750

751
// handle lookup/table switch instructions.  Called be ChangeSwitchPad class
752
bool Relocator::handle_switch_pad(int bci, int old_pad, bool is_lookup_switch) {
753
  int ilen = rc_instr_len(bci);
754
  int new_pad = align(bci+1) - (bci+1);
755
  int pad_delta = new_pad - old_pad;
756
  if (pad_delta != 0) {
757
    int len;
758
    if (!is_lookup_switch) {
759
      int low  = int_at(bci+1+old_pad+4);
760
      int high = int_at(bci+1+old_pad+8);
761
      len = high-low+1 + 3; // 3 for default, hi, lo.
762
    } else {
763
      int npairs = int_at(bci+1+old_pad+4);
764
      len = npairs*2 + 2; // 2 for default, npairs.
765
    }
766
    // Because "relocateCode" does a "changeJumps" loop,
767
    // which parses instructions to determine their length,
768
    // we need to call that before messing with the current
769
    // instruction.  Since it may also overwrite the current
770
    // instruction when moving down, remember the possibly
771
    // overwritten part.
772

773
    // Move the code following the instruction...
774
    if (!relocate_code(bci, ilen, pad_delta)) return false;
775

776
    if (pad_delta < 0) {
777
      // Move the shrunken instruction down.
778
      memmove(addr_at(bci + 1 + new_pad),
779
              addr_at(bci + 1 + old_pad),
780
              len * 4 + pad_delta);
781
      memmove(addr_at(bci + 1 + new_pad + len*4 + pad_delta),
782
              _overwrite, -pad_delta);
783
    } else {
784
      assert(pad_delta > 0, "check");
785
      // Move the expanded instruction up.
786
      memmove(addr_at(bci +1 + new_pad),
787
              addr_at(bci +1 + old_pad),
788
              len * 4);
789
      memset(addr_at(bci + 1), 0, new_pad); // pad must be 0
790
    }
791
  }
792
  return true;
793
}
794

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.