1 /*
   2  * Copyright (c) 1998, 2019, 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 "code/codeBlob.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/nmethod.hpp"
  29 #include "code/scopeDesc.hpp"
  30 #include "compiler/oopMap.hpp"
  31 #include "gc/shared/collectedHeap.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "memory/iterator.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/compressedOops.hpp"
  37 #include "runtime/frame.inline.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/signature.hpp"
  40 #include "utilities/align.hpp"
  41 #include "utilities/lockFreeStack.hpp"
  42 #ifdef COMPILER1
  43 #include "c1/c1_Defs.hpp"
  44 #endif
  45 #ifdef COMPILER2
  46 #include "opto/optoreg.hpp"
  47 #endif
  48 
  49 // OopMapStream
  50 
  51 OopMapStream::OopMapStream(OopMap* oop_map) {
  52   _stream = new CompressedReadStream(oop_map->write_stream()->buffer());
  53   _size = oop_map->omv_count();
  54   _position = 0;
  55   _valid_omv = false;
  56 }
  57 
  58 OopMapStream::OopMapStream(const ImmutableOopMap* oop_map) {
  59   _stream = new CompressedReadStream(oop_map->data_addr());
  60   _size = oop_map->count();
  61   _position = 0;
  62   _valid_omv = false;
  63 }
  64 
  65 void OopMapStream::find_next() {
  66   if (_position++ < _size) {
  67     _omv.read_from(_stream);
  68     _valid_omv = true;
  69     return;
  70   }
  71   _valid_omv = false;
  72 }
  73 
  74 
  75 // OopMap
  76 
  77 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
  78 // slots to hold 4-byte values like ints and floats in the LP64 build.
  79 OopMap::OopMap(int frame_size, int arg_count) {
  80   // OopMaps are usually quite so small, so pick a small initial size
  81   set_write_stream(new CompressedWriteStream(32));
  82   set_omv_count(0);
  83 
  84 #ifdef ASSERT
  85   _locs_length = VMRegImpl::stack2reg(0)->value() + frame_size + arg_count;
  86   _locs_used   = NEW_RESOURCE_ARRAY(OopMapValue::oop_types, _locs_length);
  87   for(int i = 0; i < _locs_length; i++) _locs_used[i] = OopMapValue::unused_value;
  88 #endif
  89 }
  90 
  91 
  92 OopMap::OopMap(OopMap::DeepCopyToken, OopMap* source) {
  93   // This constructor does a deep copy
  94   // of the source OopMap.
  95   set_write_stream(new CompressedWriteStream(source->omv_count() * 2));
  96   set_omv_count(0);
  97   set_offset(source->offset());
  98 
  99 #ifdef ASSERT
 100   _locs_length = source->_locs_length;
 101   _locs_used = NEW_RESOURCE_ARRAY(OopMapValue::oop_types, _locs_length);
 102   for(int i = 0; i < _locs_length; i++) _locs_used[i] = OopMapValue::unused_value;
 103 #endif
 104 
 105   // We need to copy the entries too.
 106   for (OopMapStream oms(source); !oms.is_done(); oms.next()) {
 107     OopMapValue omv = oms.current();
 108     omv.write_on(write_stream());
 109     increment_count();
 110   }
 111 }
 112 
 113 
 114 OopMap* OopMap::deep_copy() {
 115   return new OopMap(_deep_copy_token, this);
 116 }
 117 
 118 void OopMap::copy_data_to(address addr) const {
 119   memcpy(addr, write_stream()->buffer(), write_stream()->position());
 120 }
 121 
 122 int OopMap::heap_size() const {
 123   int size = sizeof(OopMap);
 124   int align = sizeof(void *) - 1;
 125   size += write_stream()->position();
 126   // Align to a reasonable ending point
 127   size = ((size+align) & ~align);
 128   return size;
 129 }
 130 
 131 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
 132 // slots to hold 4-byte values like ints and floats in the LP64 build.
 133 void OopMap::set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional) {
 134 
 135   assert(reg->value() < _locs_length, "too big reg value for stack size");
 136   assert( _locs_used[reg->value()] == OopMapValue::unused_value, "cannot insert twice" );
 137   debug_only( _locs_used[reg->value()] = x; )
 138 
 139   OopMapValue o(reg, x, optional);
 140   o.write_on(write_stream());
 141   increment_count();
 142 }
 143 
 144 
 145 void OopMap::set_oop(VMReg reg) {
 146   set_xxx(reg, OopMapValue::oop_value, VMRegImpl::Bad());
 147 }
 148 
 149 
 150 void OopMap::set_narrowoop(VMReg reg) {
 151   set_xxx(reg, OopMapValue::narrowoop_value, VMRegImpl::Bad());
 152 }
 153 
 154 
 155 void OopMap::set_callee_saved(VMReg reg, VMReg caller_machine_register ) {
 156   set_xxx(reg, OopMapValue::callee_saved_value, caller_machine_register);
 157 }
 158 
 159 
 160 void OopMap::set_derived_oop(VMReg reg, VMReg derived_from_local_register ) {
 161   if( reg == derived_from_local_register ) {
 162     // Actually an oop, derived shares storage with base,
 163     set_oop(reg);
 164   } else {
 165     set_xxx(reg, OopMapValue::derived_oop_value, derived_from_local_register);
 166   }
 167 }
 168 
 169 // OopMapSet
 170 
 171 OopMapSet::OopMapSet() : _list(MinOopMapAllocation) {}
 172 
 173 void OopMapSet::add_gc_map(int pc_offset, OopMap *map ) {
 174   map->set_offset(pc_offset);
 175 
 176 #ifdef ASSERT
 177   if(_list.length() > 0) {
 178     OopMap* last = _list.last();
 179     if (last->offset() == map->offset() ) {
 180       fatal("OopMap inserted twice");
 181     }
 182     if (last->offset() > map->offset()) {
 183       tty->print_cr( "WARNING, maps not sorted: pc[%d]=%d, pc[%d]=%d",
 184                       _list.length(),last->offset(),_list.length()+1,map->offset());
 185     }
 186   }
 187 #endif // ASSERT
 188 
 189   add(map);
 190 }
 191 
 192 static void add_derived_oop(oop* base, oop* derived) {
 193 #if !defined(TIERED) && !INCLUDE_JVMCI
 194   COMPILER1_PRESENT(ShouldNotReachHere();)
 195 #endif // !defined(TIERED) && !INCLUDE_JVMCI
 196 #if COMPILER2_OR_JVMCI
 197   DerivedPointerTable::add(derived, base);
 198 #endif // COMPILER2_OR_JVMCI
 199 }
 200 
 201 
 202 #ifndef PRODUCT
 203 static void trace_codeblob_maps(const frame *fr, const RegisterMap *reg_map) {
 204   // Print oopmap and regmap
 205   tty->print_cr("------ ");
 206   CodeBlob* cb = fr->cb();
 207   const ImmutableOopMapSet* maps = cb->oop_maps();
 208   const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc());
 209   map->print();
 210   if( cb->is_nmethod() ) {
 211     nmethod* nm = (nmethod*)cb;
 212     // native wrappers have no scope data, it is implied
 213     if (nm->is_native_method()) {
 214       tty->print("bci: 0 (native)");
 215     } else {
 216       ScopeDesc* scope  = nm->scope_desc_at(fr->pc());
 217       tty->print("bci: %d ",scope->bci());
 218     }
 219   }
 220   tty->cr();
 221   fr->print_on(tty);
 222   tty->print("     ");
 223   cb->print_value_on(tty);  tty->cr();
 224   reg_map->print();
 225   tty->print_cr("------ ");
 226 
 227 }
 228 #endif // PRODUCT
 229 
 230 void OopMapSet::oops_do(const frame *fr, const RegisterMap* reg_map, OopClosure* f) {
 231   // add derived oops to a table
 232   all_do(fr, reg_map, f, add_derived_oop, &do_nothing_cl);
 233 }
 234 
 235 
 236 void OopMapSet::all_do(const frame *fr, const RegisterMap *reg_map,
 237                        OopClosure* oop_fn, void derived_oop_fn(oop*, oop*),
 238                        OopClosure* value_fn) {
 239   CodeBlob* cb = fr->cb();
 240   assert(cb != NULL, "no codeblob");
 241 
 242   NOT_PRODUCT(if (TraceCodeBlobStacks) trace_codeblob_maps(fr, reg_map);)
 243 
 244   const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc());
 245   assert(map != NULL, "no ptr map found");
 246 
 247   // handle derived pointers first (otherwise base pointer may be
 248   // changed before derived pointer offset has been collected)
 249   {
 250     for (OopMapStream oms(map); !oms.is_done(); oms.next()) {
 251       OopMapValue omv = oms.current();
 252       if (omv.type() != OopMapValue::derived_oop_value) {
 253         continue;
 254       }
 255 
 256 #ifndef TIERED
 257       COMPILER1_PRESENT(ShouldNotReachHere();)
 258 #if INCLUDE_JVMCI
 259       if (UseJVMCICompiler) {
 260         ShouldNotReachHere();
 261       }
 262 #endif
 263 #endif // !TIERED
 264       oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map);
 265       guarantee(loc != NULL, "missing saved register");
 266       oop *derived_loc = loc;
 267       oop *base_loc    = fr->oopmapreg_to_location(omv.content_reg(), reg_map);
 268       // Ignore NULL oops and decoded NULL narrow oops which
 269       // equal to CompressedOops::base() when a narrow oop
 270       // implicit null check is used in compiled code.
 271       // The narrow_oop_base could be NULL or be the address
 272       // of the page below heap depending on compressed oops mode.
 273       if (base_loc != NULL && *base_loc != NULL && !CompressedOops::is_base(*base_loc)) {
 274         derived_oop_fn(base_loc, derived_loc);
 275       }
 276     }
 277   }
 278 
 279   {
 280     // We want coop and oop oop_types
 281     for (OopMapStream oms(map); !oms.is_done(); oms.next()) {
 282       OopMapValue omv = oms.current();
 283       oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map);
 284       // It should be an error if no location can be found for a
 285       // register mentioned as contained an oop of some kind.  Maybe
 286       // this was allowed previously because value_value items might
 287       // be missing?
 288       guarantee(loc != NULL, "missing saved register");
 289       if ( omv.type() == OopMapValue::oop_value ) {
 290         oop val = *loc;
 291         if (val == NULL || CompressedOops::is_base(val)) {
 292           // Ignore NULL oops and decoded NULL narrow oops which
 293           // equal to CompressedOops::base() when a narrow oop
 294           // implicit null check is used in compiled code.
 295           // The narrow_oop_base could be NULL or be the address
 296           // of the page below heap depending on compressed oops mode.
 297           continue;
 298         }
 299 #ifdef ASSERT
 300         if ((((uintptr_t)loc & (sizeof(*loc)-1)) != 0) ||
 301             !Universe::heap()->is_in_or_null(*loc)) {
 302           tty->print_cr("# Found non oop pointer.  Dumping state at failure");
 303           // try to dump out some helpful debugging information
 304           trace_codeblob_maps(fr, reg_map);
 305           omv.print();
 306           tty->print_cr("register r");
 307           omv.reg()->print();
 308           tty->print_cr("loc = %p *loc = %p\n", loc, cast_from_oop<address>(*loc));
 309           // do the real assert.
 310           assert(Universe::heap()->is_in_or_null(*loc), "found non oop pointer");
 311         }
 312 #endif // ASSERT
 313         oop_fn->do_oop(loc);
 314       } else if ( omv.type() == OopMapValue::narrowoop_value ) {
 315         narrowOop *nl = (narrowOop*)loc;
 316 #ifndef VM_LITTLE_ENDIAN
 317         VMReg vmReg = omv.reg();
 318         // Don't do this on SPARC float registers as they can be individually addressed
 319         if (!vmReg->is_stack() SPARC_ONLY(&& !vmReg->is_FloatRegister())) {
 320           // compressed oops in registers only take up 4 bytes of an
 321           // 8 byte register but they are in the wrong part of the
 322           // word so adjust loc to point at the right place.
 323           nl = (narrowOop*)((address)nl + 4);
 324         }
 325 #endif
 326         oop_fn->do_oop(nl);
 327       }
 328     }
 329   }
 330 }
 331 
 332 
 333 // Update callee-saved register info for the following frame
 334 void OopMapSet::update_register_map(const frame *fr, RegisterMap *reg_map) {
 335   ResourceMark rm;
 336   CodeBlob* cb = fr->cb();
 337   assert(cb != NULL, "no codeblob");
 338 
 339   // Any reg might be saved by a safepoint handler (see generate_handler_blob).
 340   assert( reg_map->_update_for_id == NULL || fr->is_older(reg_map->_update_for_id),
 341          "already updated this map; do not 'update' it twice!" );
 342   debug_only(reg_map->_update_for_id = fr->id());
 343 
 344   // Check if caller must update oop argument
 345   assert((reg_map->include_argument_oops() ||
 346           !cb->caller_must_gc_arguments(reg_map->thread())),
 347          "include_argument_oops should already be set");
 348 
 349   // Scan through oopmap and find location of all callee-saved registers
 350   // (we do not do update in place, since info could be overwritten)
 351 
 352   address pc = fr->pc();
 353   const ImmutableOopMap* map  = cb->oop_map_for_return_address(pc);
 354   assert(map != NULL, "no ptr map found");
 355   DEBUG_ONLY(int nof_callee = 0;)
 356 
 357   for (OopMapStream oms(map); !oms.is_done(); oms.next()) {
 358     OopMapValue omv = oms.current();
 359     if (omv.type() == OopMapValue::callee_saved_value) {
 360       VMReg reg = omv.content_reg();
 361       oop* loc = fr->oopmapreg_to_location(omv.reg(), reg_map);
 362       reg_map->set_location(reg, (address) loc);
 363       DEBUG_ONLY(nof_callee++;)
 364     }
 365   }
 366 
 367   // Check that runtime stubs save all callee-saved registers
 368 #ifdef COMPILER2
 369   assert(cb->is_compiled_by_c1() || cb->is_compiled_by_jvmci() || !cb->is_runtime_stub() ||
 370          (nof_callee >= SAVED_ON_ENTRY_REG_COUNT || nof_callee >= C_SAVED_ON_ENTRY_REG_COUNT),
 371          "must save all");
 372 #endif // COMPILER2
 373 }
 374 
 375 // Printing code is present in product build for -XX:+PrintAssembly.
 376 
 377 static
 378 void print_register_type(OopMapValue::oop_types x, VMReg optional,
 379                          outputStream* st) {
 380   switch( x ) {
 381   case OopMapValue::oop_value:
 382     st->print("Oop");
 383     break;
 384   case OopMapValue::narrowoop_value:
 385     st->print("NarrowOop");
 386     break;
 387   case OopMapValue::callee_saved_value:
 388     st->print("Callers_");
 389     optional->print_on(st);
 390     break;
 391   case OopMapValue::derived_oop_value:
 392     st->print("Derived_oop_");
 393     optional->print_on(st);
 394     break;
 395   default:
 396     ShouldNotReachHere();
 397   }
 398 }
 399 
 400 void OopMapValue::print_on(outputStream* st) const {
 401   reg()->print_on(st);
 402   st->print("=");
 403   print_register_type(type(),content_reg(),st);
 404   st->print(" ");
 405 }
 406 
 407 void OopMapValue::print() const { print_on(tty); }
 408 
 409 void ImmutableOopMap::print_on(outputStream* st) const {
 410   OopMapValue omv;
 411   st->print("ImmutableOopMap {");
 412   for(OopMapStream oms(this); !oms.is_done(); oms.next()) {
 413     omv = oms.current();
 414     omv.print_on(st);
 415   }
 416   st->print("}");
 417 }
 418 
 419 void ImmutableOopMap::print() const { print_on(tty); }
 420 
 421 void OopMap::print_on(outputStream* st) const {
 422   OopMapValue omv;
 423   st->print("OopMap {");
 424   for(OopMapStream oms((OopMap*)this); !oms.is_done(); oms.next()) {
 425     omv = oms.current();
 426     omv.print_on(st);
 427   }
 428   // Print hex offset in addition.
 429   st->print("off=%d/0x%x}", (int) offset(), (int) offset());
 430 }
 431 
 432 void OopMap::print() const { print_on(tty); }
 433 
 434 void ImmutableOopMapSet::print_on(outputStream* st) const {
 435   const ImmutableOopMap* last = NULL;
 436   const int len = count();
 437 
 438   st->print_cr("ImmutableOopMapSet contains %d OopMaps", len);
 439 
 440   for (int i = 0; i < len; i++) {
 441     const ImmutableOopMapPair* pair = pair_at(i);
 442     const ImmutableOopMap* map = pair->get_from(this);
 443     if (map != last) {
 444       st->cr();
 445       map->print_on(st);
 446       st->print(" pc offsets: ");
 447     }
 448     last = map;
 449     st->print("%d ", pair->pc_offset());
 450   }
 451   st->cr();
 452 }
 453 
 454 void ImmutableOopMapSet::print() const { print_on(tty); }
 455 
 456 void OopMapSet::print_on(outputStream* st) const {
 457   const int len = _list.length();
 458 
 459   st->print_cr("OopMapSet contains %d OopMaps", len);
 460 
 461   for( int i = 0; i < len; i++) {
 462     OopMap* m = at(i);
 463     st->print_cr("#%d ",i);
 464     m->print_on(st);
 465     st->cr();
 466   }
 467   st->cr();
 468 }
 469 
 470 void OopMapSet::print() const { print_on(tty); }
 471 
 472 bool OopMap::equals(const OopMap* other) const {
 473   if (other->_omv_count != _omv_count) {
 474     return false;
 475   }
 476   if (other->write_stream()->position() != write_stream()->position()) {
 477     return false;
 478   }
 479   if (memcmp(other->write_stream()->buffer(), write_stream()->buffer(), write_stream()->position()) != 0) {
 480     return false;
 481   }
 482   return true;
 483 }
 484 
 485 const ImmutableOopMap* ImmutableOopMapSet::find_map_at_offset(int pc_offset) const {
 486   ImmutableOopMapPair* pairs = get_pairs();
 487   ImmutableOopMapPair* last  = NULL;
 488 
 489   for (int i = 0; i < _count; ++i) {
 490     if (pairs[i].pc_offset() >= pc_offset) {
 491       last = &pairs[i];
 492       break;
 493     }
 494   }
 495 
 496   // Heal Coverity issue: potential index out of bounds access.
 497   guarantee(last != NULL, "last may not be null");
 498   assert(last->pc_offset() == pc_offset, "oopmap not found");
 499   return last->get_from(this);
 500 }
 501 
 502 const ImmutableOopMap* ImmutableOopMapPair::get_from(const ImmutableOopMapSet* set) const {
 503   return set->oopmap_at_offset(_oopmap_offset);
 504 }
 505 
 506 ImmutableOopMap::ImmutableOopMap(const OopMap* oopmap) : _count(oopmap->count()) {
 507   address addr = data_addr();
 508   oopmap->copy_data_to(addr);
 509 }
 510 
 511 #ifdef ASSERT
 512 int ImmutableOopMap::nr_of_bytes() const {
 513   OopMapStream oms(this);
 514 
 515   while (!oms.is_done()) {
 516     oms.next();
 517   }
 518   return sizeof(ImmutableOopMap) + oms.stream_position();
 519 }
 520 #endif
 521 
 522 ImmutableOopMapBuilder::ImmutableOopMapBuilder(const OopMapSet* set) : _set(set), _empty(NULL), _last(NULL), _empty_offset(-1), _last_offset(-1), _offset(0), _required(-1), _new_set(NULL) {
 523   _mapping = NEW_RESOURCE_ARRAY(Mapping, _set->size());
 524 }
 525 
 526 int ImmutableOopMapBuilder::size_for(const OopMap* map) const {
 527   return align_up((int)sizeof(ImmutableOopMap) + map->data_size(), 8);
 528 }
 529 
 530 int ImmutableOopMapBuilder::heap_size() {
 531   int base = sizeof(ImmutableOopMapSet);
 532   base = align_up(base, 8);
 533 
 534   // all of ours pc / offset pairs
 535   int pairs = _set->size() * sizeof(ImmutableOopMapPair);
 536   pairs = align_up(pairs, 8);
 537 
 538   for (int i = 0; i < _set->size(); ++i) {
 539     int size = 0;
 540     OopMap* map = _set->at(i);
 541 
 542     if (is_empty(map)) {
 543       /* only keep a single empty map in the set */
 544       if (has_empty()) {
 545         _mapping[i].set(Mapping::OOPMAP_EMPTY, _empty_offset, 0, map, _empty);
 546       } else {
 547         _empty_offset = _offset;
 548         _empty = map;
 549         size = size_for(map);
 550         _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map);
 551       }
 552     } else if (is_last_duplicate(map)) {
 553       /* if this entry is identical to the previous one, just point it there */
 554       _mapping[i].set(Mapping::OOPMAP_DUPLICATE, _last_offset, 0, map, _last);
 555     } else {
 556       /* not empty, not an identical copy of the previous entry */
 557       size = size_for(map);
 558       _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map);
 559       _last_offset = _offset;
 560       _last = map;
 561     }
 562 
 563     assert(_mapping[i]._map == map, "check");
 564     _offset += size;
 565   }
 566 
 567   int total = base + pairs + _offset;
 568   DEBUG_ONLY(total += 8);
 569   _required = total;
 570   return total;
 571 }
 572 
 573 void ImmutableOopMapBuilder::fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) {
 574   assert(offset < set->nr_of_bytes(), "check");
 575   new ((address) pair) ImmutableOopMapPair(map->offset(), offset);
 576 }
 577 
 578 int ImmutableOopMapBuilder::fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) {
 579   fill_pair(pair, map, offset, set);
 580   address addr = (address) pair->get_from(_new_set); // location of the ImmutableOopMap
 581 
 582   new (addr) ImmutableOopMap(map);
 583   return size_for(map);
 584 }
 585 
 586 void ImmutableOopMapBuilder::fill(ImmutableOopMapSet* set, int sz) {
 587   ImmutableOopMapPair* pairs = set->get_pairs();
 588 
 589   for (int i = 0; i < set->count(); ++i) {
 590     const OopMap* map = _mapping[i]._map;
 591     ImmutableOopMapPair* pair = NULL;
 592     int size = 0;
 593 
 594     if (_mapping[i]._kind == Mapping::OOPMAP_NEW) {
 595       size = fill_map(&pairs[i], map, _mapping[i]._offset, set);
 596     } else if (_mapping[i]._kind == Mapping::OOPMAP_DUPLICATE || _mapping[i]._kind == Mapping::OOPMAP_EMPTY) {
 597       fill_pair(&pairs[i], map, _mapping[i]._offset, set);
 598     }
 599 
 600     const ImmutableOopMap* nv = set->find_map_at_offset(map->offset());
 601     assert(memcmp(map->data(), nv->data_addr(), map->data_size()) == 0, "check identity");
 602   }
 603 }
 604 
 605 #ifdef ASSERT
 606 void ImmutableOopMapBuilder::verify(address buffer, int size, const ImmutableOopMapSet* set) {
 607   for (int i = 0; i < 8; ++i) {
 608     assert(buffer[size - 8 + i] == (unsigned char) 0xff, "overwritten memory check");
 609   }
 610 
 611   for (int i = 0; i < set->count(); ++i) {
 612     const ImmutableOopMapPair* pair = set->pair_at(i);
 613     assert(pair->oopmap_offset() < set->nr_of_bytes(), "check size");
 614     const ImmutableOopMap* map = pair->get_from(set);
 615     int nr_of_bytes = map->nr_of_bytes();
 616     assert(pair->oopmap_offset() + nr_of_bytes <= set->nr_of_bytes(), "check size + size");
 617   }
 618 }
 619 #endif
 620 
 621 ImmutableOopMapSet* ImmutableOopMapBuilder::generate_into(address buffer) {
 622   DEBUG_ONLY(memset(&buffer[_required-8], 0xff, 8));
 623 
 624   _new_set = new (buffer) ImmutableOopMapSet(_set, _required);
 625   fill(_new_set, _required);
 626 
 627   DEBUG_ONLY(verify(buffer, _required, _new_set));
 628 
 629   return _new_set;
 630 }
 631 
 632 ImmutableOopMapSet* ImmutableOopMapBuilder::build() {
 633   _required = heap_size();
 634 
 635   // We need to allocate a chunk big enough to hold the ImmutableOopMapSet and all of its ImmutableOopMaps
 636   address buffer = NEW_C_HEAP_ARRAY(unsigned char, _required, mtCode);
 637   return generate_into(buffer);
 638 }
 639 
 640 ImmutableOopMapSet* ImmutableOopMapSet::build_from(const OopMapSet* oopmap_set) {
 641   ResourceMark mark;
 642   ImmutableOopMapBuilder builder(oopmap_set);
 643   return builder.build();
 644 }
 645 
 646 
 647 //------------------------------DerivedPointerTable---------------------------
 648 
 649 #if COMPILER2_OR_JVMCI
 650 
 651 class DerivedPointerTable::Entry : public CHeapObj<mtCompiler> {
 652   oop* _location;   // Location of derived pointer, also pointing to base
 653   intptr_t _offset; // Offset from base pointer
 654   Entry* volatile _next;
 655 
 656   static Entry* volatile* next_ptr(Entry& entry) { return &entry._next; }
 657 
 658 public:
 659   Entry(oop* location, intptr_t offset) :
 660     _location(location), _offset(offset), _next(NULL) {}
 661 
 662   oop* location() const { return _location; }
 663   intptr_t offset() const { return _offset; }
 664   Entry* next() const { return _next; }
 665 
 666   typedef LockFreeStack<Entry, &next_ptr> List;
 667   static List* _list;
 668 };
 669 
 670 DerivedPointerTable::Entry::List* DerivedPointerTable::Entry::_list = NULL;
 671 bool DerivedPointerTable::_active = false;
 672 
 673 bool DerivedPointerTable::is_empty() {
 674   return Entry::_list == NULL || Entry::_list->empty();
 675 }
 676 
 677 void DerivedPointerTable::clear() {
 678   // The first time, we create the list.  Otherwise it should be
 679   // empty.  If not, then we have probably forgotton to call
 680   // update_pointers after last GC/Scavenge.
 681   assert (!_active, "should not be active");
 682   assert(is_empty(), "table not empty");
 683   if (Entry::_list == NULL) {
 684     void* mem = NEW_C_HEAP_OBJ(Entry::List, mtCompiler);
 685     Entry::_list = ::new (mem) Entry::List();
 686   }
 687   _active = true;
 688 }
 689 
 690 // Returns value of location as an int
 691 inline intptr_t value_of_loc(oop *pointer) {
 692   return cast_from_oop<intptr_t>((*pointer));
 693 }
 694 
 695 void DerivedPointerTable::add(oop *derived_loc, oop *base_loc) {
 696   assert(Universe::heap()->is_in_or_null(*base_loc), "not an oop");
 697   assert(derived_loc != base_loc, "Base and derived in same location");
 698   if (_active) {
 699     assert(*derived_loc != (void*)base_loc, "location already added");
 700     assert(Entry::_list != NULL, "list must exist");
 701     intptr_t offset = value_of_loc(derived_loc) - value_of_loc(base_loc);
 702     // This assert is invalid because derived pointers can be
 703     // arbitrarily far away from their base.
 704     // assert(offset >= -1000000, "wrong derived pointer info");
 705 
 706     if (TraceDerivedPointers) {
 707       tty->print_cr(
 708         "Add derived pointer@" INTPTR_FORMAT
 709         " - Derived: " INTPTR_FORMAT
 710         " Base: " INTPTR_FORMAT " (@" INTPTR_FORMAT ") (Offset: " INTX_FORMAT ")",
 711         p2i(derived_loc), p2i(*derived_loc), p2i(*base_loc), p2i(base_loc), offset
 712       );
 713     }
 714     // Set derived oop location to point to base.
 715     *derived_loc = (oop)base_loc;
 716     Entry* entry = new Entry(derived_loc, offset);
 717     Entry::_list->push(*entry);
 718   }
 719 }
 720 
 721 void DerivedPointerTable::update_pointers() {
 722   assert(Entry::_list != NULL, "list must exist");
 723   Entry* entries = Entry::_list->pop_all();
 724   while (entries != NULL) {
 725     Entry* entry = entries;
 726     entries = entry->next();
 727     oop* derived_loc = entry->location();
 728     intptr_t offset  = entry->offset();
 729     // The derived oop was setup to point to location of base
 730     oop base = **(oop**)derived_loc;
 731     assert(Universe::heap()->is_in_or_null(base), "must be an oop");
 732 
 733     *derived_loc = (oop)(cast_from_oop<address>(base) + offset);
 734     assert(value_of_loc(derived_loc) - value_of_loc(&base) == offset, "sanity check");
 735 
 736     if (TraceDerivedPointers) {
 737       tty->print_cr("Updating derived pointer@" INTPTR_FORMAT
 738                     " - Derived: " INTPTR_FORMAT "  Base: " INTPTR_FORMAT " (Offset: " INTX_FORMAT ")",
 739           p2i(derived_loc), p2i(*derived_loc), p2i(base), offset);
 740     }
 741 
 742     // Delete entry
 743     delete entry;
 744   }
 745   assert(Entry::_list->empty(), "invariant");
 746   _active = false;
 747 }
 748 
 749 #endif // COMPILER2_OR_JVMCI