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