1 /*
   2  * Copyright (c) 1997, 2016, 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/altHashing.hpp"
  27 #include "classfile/compactHashtable.inline.hpp"
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "classfile/stringTable.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "gc/shared/collectedHeap.inline.hpp"
  32 #include "gc/shared/gcLocker.inline.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/filemap.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/atomic.inline.hpp"
  37 #include "runtime/mutexLocker.hpp"
  38 #include "utilities/hashtable.inline.hpp"
  39 #include "utilities/macros.hpp"
  40 #if INCLUDE_ALL_GCS
  41 #include "gc/g1/g1CollectedHeap.hpp"
  42 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  43 #include "gc/g1/g1StringDedup.hpp"
  44 #endif
  45 
  46 // the number of buckets a thread claims
  47 const int ClaimChunkSize = 32;
  48 
  49 #ifdef ASSERT
  50 class StableMemoryChecker : public StackObj {
  51   enum { _bufsize = wordSize*4 };
  52 
  53   address _region;
  54   jint    _size;
  55   u1      _save_buf[_bufsize];
  56 
  57   int sample(u1* save_buf) {
  58     if (_size <= _bufsize) {
  59       memcpy(save_buf, _region, _size);
  60       return _size;
  61     } else {
  62       // copy head and tail
  63       memcpy(&save_buf[0],          _region,                      _bufsize/2);
  64       memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
  65       return (_bufsize/2)*2;
  66     }
  67   }
  68 
  69  public:
  70   StableMemoryChecker(const void* region, jint size) {
  71     _region = (address) region;
  72     _size   = size;
  73     sample(_save_buf);
  74   }
  75 
  76   bool verify() {
  77     u1 check_buf[sizeof(_save_buf)];
  78     int check_size = sample(check_buf);
  79     return (0 == memcmp(_save_buf, check_buf, check_size));
  80   }
  81 
  82   void set_region(const void* region) { _region = (address) region; }
  83 };
  84 #endif
  85 
  86 
  87 // --------------------------------------------------------------------------
  88 StringTable* StringTable::_the_table = NULL;
  89 bool StringTable::_ignore_shared_strings = false;
  90 bool StringTable::_needs_rehashing = false;
  91 
  92 volatile int StringTable::_parallel_claimed_idx = 0;
  93 
  94 CompactHashtable<oop, char> StringTable::_shared_table;
  95 
  96 // Pick hashing algorithm
  97 template<typename T>
  98 unsigned int StringTable::hash_string(const T* s, int len) {
  99   return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) :
 100                                     java_lang_String::hash_code(s, len);
 101 }
 102 
 103 // Explicit instantiation for all supported types.
 104 template unsigned int StringTable::hash_string<jchar>(const jchar* s, int len);
 105 template unsigned int StringTable::hash_string<jbyte>(const jbyte* s, int len);
 106 
 107 oop StringTable::lookup_shared(jchar* name, int len) {
 108   // java_lang_String::hash_code() was used to compute hash values in the shared table. Don't
 109   // use the hash value from StringTable::hash_string() as it might use alternate hashcode.
 110   return _shared_table.lookup((const char*)name,
 111                               java_lang_String::hash_code(name, len), len);
 112 }
 113 
 114 oop StringTable::lookup_in_main_table(int index, jchar* name,
 115                                 int len, unsigned int hash) {
 116   int count = 0;
 117   for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) {
 118     count++;
 119     if (l->hash() == hash) {
 120       if (java_lang_String::equals(l->literal(), name, len)) {
 121         return l->literal();
 122       }
 123     }
 124   }
 125   // If the bucket size is too deep check if this hash code is insufficient.
 126   if (count >= rehash_count && !needs_rehashing()) {
 127     _needs_rehashing = check_rehash_table(count);
 128   }
 129   return NULL;
 130 }
 131 
 132 
 133 oop StringTable::basic_add(int index_arg, Handle string, jchar* name,
 134                            int len, unsigned int hashValue_arg, TRAPS) {
 135 
 136   assert(java_lang_String::equals(string(), name, len),
 137          "string must be properly initialized");
 138   // Cannot hit a safepoint in this function because the "this" pointer can move.
 139   NoSafepointVerifier nsv;
 140 
 141   // Check if the symbol table has been rehashed, if so, need to recalculate
 142   // the hash value and index before second lookup.
 143   unsigned int hashValue;
 144   int index;
 145   if (use_alternate_hashcode()) {
 146     hashValue = hash_string(name, len);
 147     index = hash_to_index(hashValue);
 148   } else {
 149     hashValue = hashValue_arg;
 150     index = index_arg;
 151   }
 152 
 153   // Since look-up was done lock-free, we need to check if another
 154   // thread beat us in the race to insert the symbol.
 155 
 156   // No need to lookup the shared table from here since the caller (intern()) already did
 157   oop test = lookup_in_main_table(index, name, len, hashValue); // calls lookup(u1*, int)
 158   if (test != NULL) {
 159     // Entry already added
 160     return test;
 161   }
 162 
 163   HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string());
 164   add_entry(index, entry);
 165   return string();
 166 }
 167 
 168 
 169 oop StringTable::lookup(Symbol* symbol) {
 170   ResourceMark rm;
 171   int length;
 172   jchar* chars = symbol->as_unicode(length);
 173   return lookup(chars, length);
 174 }
 175 
 176 // Tell the GC that this string was looked up in the StringTable.
 177 static void ensure_string_alive(oop string) {
 178   // A lookup in the StringTable could return an object that was previously
 179   // considered dead. The SATB part of G1 needs to get notified about this
 180   // potential resurrection, otherwise the marking might not find the object.
 181 #if INCLUDE_ALL_GCS
 182   if (UseG1GC && string != NULL) {
 183     G1SATBCardTableModRefBS::enqueue(string);
 184   }
 185 #endif
 186 }
 187 
 188 oop StringTable::lookup(jchar* name, int len) {
 189   oop string = lookup_shared(name, len);
 190   if (string != NULL) {
 191     return string;
 192   }
 193 
 194   unsigned int hash = hash_string(name, len);
 195   int index = the_table()->hash_to_index(hash);
 196   string = the_table()->lookup_in_main_table(index, name, len, hash);
 197 
 198   ensure_string_alive(string);
 199 
 200   return string;
 201 }
 202 
 203 oop StringTable::intern(Handle string_or_null, jchar* name,
 204                         int len, TRAPS) {
 205   oop found_string = lookup_shared(name, len);
 206   if (found_string != NULL) {
 207     return found_string;
 208   }
 209 
 210   unsigned int hashValue = hash_string(name, len);
 211   int index = the_table()->hash_to_index(hashValue);
 212   found_string = the_table()->lookup_in_main_table(index, name, len, hashValue);
 213 
 214   // Found
 215   if (found_string != NULL) {
 216     if (found_string != string_or_null()) {
 217       ensure_string_alive(found_string);
 218     }
 219     return found_string;
 220   }
 221 
 222   debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
 223   assert(!Universe::heap()->is_in_reserved(name),
 224          "proposed name of symbol must be stable");
 225 
 226   Handle string;
 227   // try to reuse the string if possible
 228   if (!string_or_null.is_null()) {
 229     string = string_or_null;
 230   } else {
 231     string = java_lang_String::create_from_unicode(name, len, CHECK_NULL);
 232   }
 233 
 234 #if INCLUDE_ALL_GCS
 235   if (G1StringDedup::is_enabled()) {
 236     // Deduplicate the string before it is interned. Note that we should never
 237     // deduplicate a string after it has been interned. Doing so will counteract
 238     // compiler optimizations done on e.g. interned string literals.
 239     G1StringDedup::deduplicate(string());
 240   }
 241 #endif
 242 
 243   // Grab the StringTable_lock before getting the_table() because it could
 244   // change at safepoint.
 245   oop added_or_found;
 246   {
 247     MutexLocker ml(StringTable_lock, THREAD);
 248     // Otherwise, add to symbol to table
 249     added_or_found = the_table()->basic_add(index, string, name, len,
 250                                   hashValue, CHECK_NULL);
 251   }
 252 
 253   if (added_or_found != string()) { 
 254     ensure_string_alive(added_or_found);
 255   }
 256 
 257   return added_or_found;
 258 }
 259 
 260 oop StringTable::intern(Symbol* symbol, TRAPS) {
 261   if (symbol == NULL) return NULL;
 262   ResourceMark rm(THREAD);
 263   int length;
 264   jchar* chars = symbol->as_unicode(length);
 265   Handle string;
 266   oop result = intern(string, chars, length, CHECK_NULL);
 267   return result;
 268 }
 269 
 270 
 271 oop StringTable::intern(oop string, TRAPS)
 272 {
 273   if (string == NULL) return NULL;
 274   ResourceMark rm(THREAD);
 275   int length;
 276   Handle h_string (THREAD, string);
 277   jchar* chars = java_lang_String::as_unicode_string(string, length, CHECK_NULL);
 278   oop result = intern(h_string, chars, length, CHECK_NULL);
 279   return result;
 280 }
 281 
 282 
 283 oop StringTable::intern(const char* utf8_string, TRAPS) {
 284   if (utf8_string == NULL) return NULL;
 285   ResourceMark rm(THREAD);
 286   int length = UTF8::unicode_length(utf8_string);
 287   jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
 288   UTF8::convert_to_unicode(utf8_string, chars, length);
 289   Handle string;
 290   oop result = intern(string, chars, length, CHECK_NULL);
 291   return result;
 292 }
 293 
 294 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) {
 295   buckets_unlink_or_oops_do(is_alive, f, 0, the_table()->table_size(), processed, removed);
 296 }
 297 
 298 void StringTable::possibly_parallel_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) {
 299   // Readers of the table are unlocked, so we should only be removing
 300   // entries at a safepoint.
 301   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 302   const int limit = the_table()->table_size();
 303 
 304   for (;;) {
 305     // Grab next set of buckets to scan
 306     int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize;
 307     if (start_idx >= limit) {
 308       // End of table
 309       break;
 310     }
 311 
 312     int end_idx = MIN2(limit, start_idx + ClaimChunkSize);
 313     buckets_unlink_or_oops_do(is_alive, f, start_idx, end_idx, processed, removed);
 314   }
 315 }
 316 
 317 void StringTable::buckets_oops_do(OopClosure* f, int start_idx, int end_idx) {
 318   const int limit = the_table()->table_size();
 319 
 320   assert(0 <= start_idx && start_idx <= limit,
 321          "start_idx (%d) is out of bounds", start_idx);
 322   assert(0 <= end_idx && end_idx <= limit,
 323          "end_idx (%d) is out of bounds", end_idx);
 324   assert(start_idx <= end_idx,
 325          "Index ordering: start_idx=%d, end_idx=%d",
 326          start_idx, end_idx);
 327 
 328   for (int i = start_idx; i < end_idx; i += 1) {
 329     HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i);
 330     while (entry != NULL) {
 331       assert(!entry->is_shared(), "CDS not used for the StringTable");
 332 
 333       f->do_oop((oop*)entry->literal_addr());
 334 
 335       entry = entry->next();
 336     }
 337   }
 338 }
 339 
 340 void StringTable::buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, int* processed, int* removed) {
 341   const int limit = the_table()->table_size();
 342 
 343   assert(0 <= start_idx && start_idx <= limit,
 344          "start_idx (%d) is out of bounds", start_idx);
 345   assert(0 <= end_idx && end_idx <= limit,
 346          "end_idx (%d) is out of bounds", end_idx);
 347   assert(start_idx <= end_idx,
 348          "Index ordering: start_idx=%d, end_idx=%d",
 349          start_idx, end_idx);
 350 
 351   for (int i = start_idx; i < end_idx; ++i) {
 352     HashtableEntry<oop, mtSymbol>** p = the_table()->bucket_addr(i);
 353     HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i);
 354     while (entry != NULL) {
 355       assert(!entry->is_shared(), "CDS not used for the StringTable");
 356 
 357       if (is_alive->do_object_b(entry->literal())) {
 358         if (f != NULL) {
 359           f->do_oop((oop*)entry->literal_addr());
 360         }
 361         p = entry->next_addr();
 362       } else {
 363         *p = entry->next();
 364         the_table()->free_entry(entry);
 365         (*removed)++;
 366       }
 367       (*processed)++;
 368       entry = *p;
 369     }
 370   }
 371 }
 372 
 373 void StringTable::oops_do(OopClosure* f) {
 374   buckets_oops_do(f, 0, the_table()->table_size());
 375 }
 376 
 377 void StringTable::possibly_parallel_oops_do(OopClosure* f) {
 378   const int limit = the_table()->table_size();
 379 
 380   for (;;) {
 381     // Grab next set of buckets to scan
 382     int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize;
 383     if (start_idx >= limit) {
 384       // End of table
 385       break;
 386     }
 387 
 388     int end_idx = MIN2(limit, start_idx + ClaimChunkSize);
 389     buckets_oops_do(f, start_idx, end_idx);
 390   }
 391 }
 392 
 393 // This verification is part of Universe::verify() and needs to be quick.
 394 // See StringTable::verify_and_compare() below for exhaustive verification.
 395 void StringTable::verify() {
 396   for (int i = 0; i < the_table()->table_size(); ++i) {
 397     HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i);
 398     for ( ; p != NULL; p = p->next()) {
 399       oop s = p->literal();
 400       guarantee(s != NULL, "interned string is NULL");
 401       unsigned int h = java_lang_String::hash_string(s);
 402       guarantee(p->hash() == h, "broken hash in string table entry");
 403       guarantee(the_table()->hash_to_index(h) == i,
 404                 "wrong index in string table");
 405     }
 406   }
 407 }
 408 
 409 void StringTable::dump(outputStream* st, bool verbose) {
 410   if (!verbose) {
 411     the_table()->dump_table(st, "StringTable");
 412   } else {
 413     Thread* THREAD = Thread::current();
 414     st->print_cr("VERSION: 1.1");
 415     for (int i = 0; i < the_table()->table_size(); ++i) {
 416       HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i);
 417       for ( ; p != NULL; p = p->next()) {
 418         oop s = p->literal();
 419         typeArrayOop value  = java_lang_String::value(s);
 420         int          length = java_lang_String::length(s);
 421         bool      is_latin1 = java_lang_String::is_latin1(s);
 422 
 423         if (length <= 0) {
 424           st->print("%d: ", length);
 425         } else {
 426           ResourceMark rm(THREAD);
 427           int utf8_length;
 428           char* utf8_string;
 429 
 430           if (!is_latin1) {
 431             jchar* chars = value->char_at_addr(0);
 432             utf8_length = UNICODE::utf8_length(chars, length);
 433             utf8_string = UNICODE::as_utf8(chars, length);
 434           } else {
 435             jbyte* bytes = value->byte_at_addr(0);
 436             utf8_length = UNICODE::utf8_length(bytes, length);
 437             utf8_string = UNICODE::as_utf8(bytes, length);
 438           }
 439 
 440           st->print("%d: ", utf8_length);
 441           HashtableTextDump::put_utf8(st, utf8_string, utf8_length);
 442         }
 443         st->cr();
 444       }
 445     }
 446   }
 447 }
 448 
 449 StringTable::VerifyRetTypes StringTable::compare_entries(
 450                                       int bkt1, int e_cnt1,
 451                                       HashtableEntry<oop, mtSymbol>* e_ptr1,
 452                                       int bkt2, int e_cnt2,
 453                                       HashtableEntry<oop, mtSymbol>* e_ptr2) {
 454   // These entries are sanity checked by verify_and_compare_entries()
 455   // before this function is called.
 456   oop str1 = e_ptr1->literal();
 457   oop str2 = e_ptr2->literal();
 458 
 459   if (str1 == str2) {
 460     tty->print_cr("ERROR: identical oop values (0x" PTR_FORMAT ") "
 461                   "in entry @ bucket[%d][%d] and entry @ bucket[%d][%d]",
 462                   p2i(str1), bkt1, e_cnt1, bkt2, e_cnt2);
 463     return _verify_fail_continue;
 464   }
 465 
 466   if (java_lang_String::equals(str1, str2)) {
 467     tty->print_cr("ERROR: identical String values in entry @ "
 468                   "bucket[%d][%d] and entry @ bucket[%d][%d]",
 469                   bkt1, e_cnt1, bkt2, e_cnt2);
 470     return _verify_fail_continue;
 471   }
 472 
 473   return _verify_pass;
 474 }
 475 
 476 StringTable::VerifyRetTypes StringTable::verify_entry(int bkt, int e_cnt,
 477                                       HashtableEntry<oop, mtSymbol>* e_ptr,
 478                                       StringTable::VerifyMesgModes mesg_mode) {
 479 
 480   VerifyRetTypes ret = _verify_pass;  // be optimistic
 481 
 482   oop str = e_ptr->literal();
 483   if (str == NULL) {
 484     if (mesg_mode == _verify_with_mesgs) {
 485       tty->print_cr("ERROR: NULL oop value in entry @ bucket[%d][%d]", bkt,
 486                     e_cnt);
 487     }
 488     // NULL oop means no more verifications are possible
 489     return _verify_fail_done;
 490   }
 491 
 492   if (str->klass() != SystemDictionary::String_klass()) {
 493     if (mesg_mode == _verify_with_mesgs) {
 494       tty->print_cr("ERROR: oop is not a String in entry @ bucket[%d][%d]",
 495                     bkt, e_cnt);
 496     }
 497     // not a String means no more verifications are possible
 498     return _verify_fail_done;
 499   }
 500 
 501   unsigned int h = java_lang_String::hash_string(str);
 502   if (e_ptr->hash() != h) {
 503     if (mesg_mode == _verify_with_mesgs) {
 504       tty->print_cr("ERROR: broken hash value in entry @ bucket[%d][%d], "
 505                     "bkt_hash=%d, str_hash=%d", bkt, e_cnt, e_ptr->hash(), h);
 506     }
 507     ret = _verify_fail_continue;
 508   }
 509 
 510   if (the_table()->hash_to_index(h) != bkt) {
 511     if (mesg_mode == _verify_with_mesgs) {
 512       tty->print_cr("ERROR: wrong index value for entry @ bucket[%d][%d], "
 513                     "str_hash=%d, hash_to_index=%d", bkt, e_cnt, h,
 514                     the_table()->hash_to_index(h));
 515     }
 516     ret = _verify_fail_continue;
 517   }
 518 
 519   return ret;
 520 }
 521 
 522 // See StringTable::verify() above for the quick verification that is
 523 // part of Universe::verify(). This verification is exhaustive and
 524 // reports on every issue that is found. StringTable::verify() only
 525 // reports on the first issue that is found.
 526 //
 527 // StringTable::verify_entry() checks:
 528 // - oop value != NULL (same as verify())
 529 // - oop value is a String
 530 // - hash(String) == hash in entry (same as verify())
 531 // - index for hash == index of entry (same as verify())
 532 //
 533 // StringTable::compare_entries() checks:
 534 // - oops are unique across all entries
 535 // - String values are unique across all entries
 536 //
 537 int StringTable::verify_and_compare_entries() {
 538   assert(StringTable_lock->is_locked(), "sanity check");
 539 
 540   int  fail_cnt = 0;
 541 
 542   // first, verify all the entries individually:
 543   for (int bkt = 0; bkt < the_table()->table_size(); bkt++) {
 544     HashtableEntry<oop, mtSymbol>* e_ptr = the_table()->bucket(bkt);
 545     for (int e_cnt = 0; e_ptr != NULL; e_ptr = e_ptr->next(), e_cnt++) {
 546       VerifyRetTypes ret = verify_entry(bkt, e_cnt, e_ptr, _verify_with_mesgs);
 547       if (ret != _verify_pass) {
 548         fail_cnt++;
 549       }
 550     }
 551   }
 552 
 553   // Optimization: if the above check did not find any failures, then
 554   // the comparison loop below does not need to call verify_entry()
 555   // before calling compare_entries(). If there were failures, then we
 556   // have to call verify_entry() to see if the entry can be passed to
 557   // compare_entries() safely. When we call verify_entry() in the loop
 558   // below, we do so quietly to void duplicate messages and we don't
 559   // increment fail_cnt because the failures have already been counted.
 560   bool need_entry_verify = (fail_cnt != 0);
 561 
 562   // second, verify all entries relative to each other:
 563   for (int bkt1 = 0; bkt1 < the_table()->table_size(); bkt1++) {
 564     HashtableEntry<oop, mtSymbol>* e_ptr1 = the_table()->bucket(bkt1);
 565     for (int e_cnt1 = 0; e_ptr1 != NULL; e_ptr1 = e_ptr1->next(), e_cnt1++) {
 566       if (need_entry_verify) {
 567         VerifyRetTypes ret = verify_entry(bkt1, e_cnt1, e_ptr1,
 568                                           _verify_quietly);
 569         if (ret == _verify_fail_done) {
 570           // cannot use the current entry to compare against other entries
 571           continue;
 572         }
 573       }
 574 
 575       for (int bkt2 = bkt1; bkt2 < the_table()->table_size(); bkt2++) {
 576         HashtableEntry<oop, mtSymbol>* e_ptr2 = the_table()->bucket(bkt2);
 577         int e_cnt2;
 578         for (e_cnt2 = 0; e_ptr2 != NULL; e_ptr2 = e_ptr2->next(), e_cnt2++) {
 579           if (bkt1 == bkt2 && e_cnt2 <= e_cnt1) {
 580             // skip the entries up to and including the one that
 581             // we're comparing against
 582             continue;
 583           }
 584 
 585           if (need_entry_verify) {
 586             VerifyRetTypes ret = verify_entry(bkt2, e_cnt2, e_ptr2,
 587                                               _verify_quietly);
 588             if (ret == _verify_fail_done) {
 589               // cannot compare against this entry
 590               continue;
 591             }
 592           }
 593 
 594           // compare two entries, report and count any failures:
 595           if (compare_entries(bkt1, e_cnt1, e_ptr1, bkt2, e_cnt2, e_ptr2)
 596               != _verify_pass) {
 597             fail_cnt++;
 598           }
 599         }
 600       }
 601     }
 602   }
 603   return fail_cnt;
 604 }
 605 
 606 // Create a new table and using alternate hash code, populate the new table
 607 // with the existing strings.   Set flag to use the alternate hash code afterwards.
 608 void StringTable::rehash_table() {
 609   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 610   // This should never happen with -Xshare:dump but it might in testing mode.
 611   if (DumpSharedSpaces) return;
 612   StringTable* new_table = new StringTable();
 613 
 614   // Rehash the table
 615   the_table()->move_to(new_table);
 616 
 617   // Delete the table and buckets (entries are reused in new table).
 618   delete _the_table;
 619   // Don't check if we need rehashing until the table gets unbalanced again.
 620   // Then rehash with a new global seed.
 621   _needs_rehashing = false;
 622   _the_table = new_table;
 623 }
 624 
 625 // Utility for dumping strings
 626 StringtableDCmd::StringtableDCmd(outputStream* output, bool heap) :
 627                                  DCmdWithParser(output, heap),
 628   _verbose("-verbose", "Dump the content of each string in the table",
 629            "BOOLEAN", false, "false") {
 630   _dcmdparser.add_dcmd_option(&_verbose);
 631 }
 632 
 633 void StringtableDCmd::execute(DCmdSource source, TRAPS) {
 634   VM_DumpHashtable dumper(output(), VM_DumpHashtable::DumpStrings,
 635                          _verbose.value());
 636   VMThread::execute(&dumper);
 637 }
 638 
 639 int StringtableDCmd::num_arguments() {
 640   ResourceMark rm;
 641   StringtableDCmd* dcmd = new StringtableDCmd(NULL, false);
 642   if (dcmd != NULL) {
 643     DCmdMark mark(dcmd);
 644     return dcmd->_dcmdparser.num_arguments();
 645   } else {
 646     return 0;
 647   }
 648 }
 649 
 650 // Sharing
 651 bool StringTable::copy_shared_string(GrowableArray<MemRegion> *string_space,
 652                                      CompactHashtableWriter* ch_table) {
 653 #if INCLUDE_CDS && INCLUDE_ALL_GCS && defined(_LP64) && !defined(_WINDOWS)
 654   assert(UseG1GC, "Only support G1 GC");
 655   assert(UseCompressedOops && UseCompressedClassPointers,
 656          "Only support UseCompressedOops and UseCompressedClassPointers enabled");
 657 
 658   Thread* THREAD = Thread::current();
 659   G1CollectedHeap::heap()->begin_archive_alloc_range();
 660   for (int i = 0; i < the_table()->table_size(); ++i) {
 661     HashtableEntry<oop, mtSymbol>* bucket = the_table()->bucket(i);
 662     for ( ; bucket != NULL; bucket = bucket->next()) {
 663       oop s = bucket->literal();
 664       unsigned int hash = java_lang_String::hash_code(s);
 665       if (hash == 0) {
 666         continue;
 667       }
 668 
 669       // allocate the new 'value' array first
 670       typeArrayOop v = java_lang_String::value(s);
 671       int v_len = v->size();
 672       typeArrayOop new_v;
 673       if (G1CollectedHeap::heap()->is_archive_alloc_too_large(v_len)) {
 674         continue; // skip the current String. The 'value' array is too large to handle
 675       } else {
 676         new_v = (typeArrayOop)G1CollectedHeap::heap()->archive_mem_allocate(v_len);
 677         if (new_v == NULL) {
 678           return false; // allocation failed
 679         }
 680       }
 681       // now allocate the new String object
 682       int s_len = s->size();
 683       oop new_s = (oop)G1CollectedHeap::heap()->archive_mem_allocate(s_len);
 684       if (new_s == NULL) {
 685         return false;
 686       }
 687 
 688       s->identity_hash();
 689       v->identity_hash();
 690 
 691       // copy the objects' data
 692       Copy::aligned_disjoint_words((HeapWord*)s, (HeapWord*)new_s, s_len);
 693       Copy::aligned_disjoint_words((HeapWord*)v, (HeapWord*)new_v, v_len);
 694 
 695       // adjust the pointer to the 'value' field in the new String oop. Also pre-compute and set the
 696       // 'hash' field. That avoids "write" to the shared strings at runtime by the deduplication process.
 697       java_lang_String::set_value_raw(new_s, new_v);
 698       if (java_lang_String::hash(new_s) == 0) {
 699         java_lang_String::set_hash(new_s, hash);
 700       }
 701 
 702       // add to the compact table
 703       ch_table->add(hash, new_s);
 704     }
 705   }
 706 
 707   G1CollectedHeap::heap()->end_archive_alloc_range(string_space, os::vm_allocation_granularity());
 708   assert(string_space->length() <= 2, "sanity");
 709 #endif
 710   return true;
 711 }
 712 
 713 bool StringTable::copy_compact_table(char** top, char *end, GrowableArray<MemRegion> *string_space,
 714                                      size_t* space_size) {
 715 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS)
 716   if (!(UseG1GC && UseCompressedOops && UseCompressedClassPointers)) {
 717     if (PrintSharedSpaces) {
 718       tty->print_cr("Shared strings are excluded from the archive as UseG1GC, "
 719                     "UseCompressedOops and UseCompressedClassPointers are required.");
 720     }
 721     return true;
 722   }
 723 
 724   CompactHashtableWriter ch_table(CompactHashtable<oop, char>::_string_table,
 725                                   the_table()->number_of_entries(),
 726                                   &MetaspaceShared::stats()->string);
 727 
 728   // Copy the interned strings into the "string space" within the java heap
 729   if (!copy_shared_string(string_space, &ch_table)) {
 730     return false;
 731   }
 732 
 733   for (int i = 0; i < string_space->length(); i++) {
 734     *space_size += string_space->at(i).byte_size();
 735   }
 736 
 737   // Now dump the compact table
 738   if (*top + ch_table.get_required_bytes() > end) {
 739     // not enough space left
 740     return false;
 741   }
 742   ch_table.dump(top, end);
 743   *top = (char*)align_ptr_up(*top, sizeof(void*));
 744 
 745 #endif
 746   return true;
 747 }
 748 
 749 void StringTable::shared_oops_do(OopClosure* f) {
 750 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS)
 751   _shared_table.oops_do(f);
 752 #endif
 753 }
 754 
 755 const char* StringTable::init_shared_table(FileMapInfo *mapinfo, char *buffer) {
 756 #if INCLUDE_CDS && defined(_LP64) && !defined(_WINDOWS)
 757   if (mapinfo->space_capacity(MetaspaceShared::first_string) == 0) {
 758     // no shared string data
 759     return buffer;
 760   }
 761 
 762   // initialize the shared table
 763   juint *p = (juint*)buffer;
 764   const char* end = _shared_table.init(
 765           CompactHashtable<oop, char>::_string_table, (char*)p);
 766   const char* aligned_end = (const char*)align_ptr_up(end, sizeof(void*));
 767 
 768   if (_ignore_shared_strings) {
 769     _shared_table.reset();
 770   }
 771 
 772   return aligned_end;
 773 #endif
 774 
 775   return buffer;
 776 }