1 /*
   2  * Copyright (c) 1997, 2018, 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 "logging/log.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/filemap.hpp"
  35 #include "memory/metaspaceShared.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/access.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "oops/typeArrayOop.inline.hpp"
  40 #include "runtime/atomic.hpp"
  41 #include "runtime/handles.inline.hpp"
  42 #include "runtime/mutexLocker.hpp"
  43 #include "runtime/safepointVerifiers.hpp"
  44 #include "services/diagnosticCommand.hpp"
  45 #include "utilities/hashtable.inline.hpp"
  46 #include "utilities/macros.hpp"
  47 #if INCLUDE_G1GC
  48 #include "gc/g1/g1StringDedup.hpp"
  49 #endif
  50 
  51 // the number of buckets a thread claims
  52 const int ClaimChunkSize = 32;
  53 
  54 #ifdef ASSERT
  55 class StableMemoryChecker : public StackObj {
  56   enum { _bufsize = wordSize*4 };
  57 
  58   address _region;
  59   jint    _size;
  60   u1      _save_buf[_bufsize];
  61 
  62   int sample(u1* save_buf) {
  63     if (_size <= _bufsize) {
  64       memcpy(save_buf, _region, _size);
  65       return _size;
  66     } else {
  67       // copy head and tail
  68       memcpy(&save_buf[0],          _region,                      _bufsize/2);
  69       memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
  70       return (_bufsize/2)*2;
  71     }
  72   }
  73 
  74  public:
  75   StableMemoryChecker(const void* region, jint size) {
  76     _region = (address) region;
  77     _size   = size;
  78     sample(_save_buf);
  79   }
  80 
  81   bool verify() {
  82     u1 check_buf[sizeof(_save_buf)];
  83     int check_size = sample(check_buf);
  84     return (0 == memcmp(_save_buf, check_buf, check_size));
  85   }
  86 
  87   void set_region(const void* region) { _region = (address) region; }
  88 };
  89 #endif
  90 
  91 
  92 // --------------------------------------------------------------------------
  93 StringTable* StringTable::_the_table = NULL;
  94 bool StringTable::_shared_string_mapped = false;
  95 bool StringTable::_needs_rehashing = false;
  96 
  97 volatile int StringTable::_parallel_claimed_idx = 0;
  98 
  99 CompactHashtable<oop, char> StringTable::_shared_table;
 100 
 101 // Pick hashing algorithm
 102 unsigned int StringTable::hash_string(const jchar* s, int len) {
 103   return use_alternate_hashcode() ? alt_hash_string(s, len) :
 104                                     java_lang_String::hash_code(s, len);
 105 }
 106 
 107 unsigned int StringTable::alt_hash_string(const jchar* s, int len) {
 108   return AltHashing::murmur3_32(seed(), s, len);
 109 }
 110 
 111 unsigned int StringTable::hash_string(oop string) {
 112   EXCEPTION_MARK;
 113   if (string == NULL) {
 114     return hash_string((jchar*)NULL, 0);
 115   }
 116   ResourceMark rm(THREAD);
 117   // All String oops are hashed as unicode
 118   int length;
 119   jchar* chars = java_lang_String::as_unicode_string(string, length, THREAD);
 120   if (chars != NULL) {
 121     return hash_string(chars, length);
 122   } else {
 123     vm_exit_out_of_memory(length, OOM_MALLOC_ERROR, "unable to create Unicode string for verification");
 124     return 0;
 125   }
 126 }
 127 
 128 oop StringTable::string_object(HashtableEntry<oop, mtSymbol>* entry) {
 129   return RootAccess<ON_PHANTOM_OOP_REF>::oop_load(entry->literal_addr());
 130 }
 131 
 132 oop StringTable::string_object_no_keepalive(HashtableEntry<oop, mtSymbol>* entry) {
 133   // The AS_NO_KEEPALIVE peeks at the oop without keeping it alive.
 134   // This is *very dangerous* in general but is okay in this specific
 135   // case. The subsequent oop_load keeps the oop alive if it it matched
 136   // the jchar* string.
 137   return RootAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(entry->literal_addr());
 138 }
 139 
 140 void StringTable::set_string_object(HashtableEntry<oop, mtSymbol>* entry, oop string) {
 141   RootAccess<ON_PHANTOM_OOP_REF>::oop_store(entry->literal_addr(), string);
 142 }
 143 
 144 oop StringTable::lookup_shared(jchar* name, int len, unsigned int hash) {
 145   assert(hash == java_lang_String::hash_code(name, len),
 146          "hash must be computed using java_lang_String::hash_code");
 147   return _shared_table.lookup((const char*)name, hash, len);
 148 }
 149 
 150 oop StringTable::lookup_in_main_table(int index, jchar* name,
 151                                       int len, unsigned int hash) {
 152   int count = 0;
 153   for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) {
 154     count++;
 155     if (l->hash() == hash) {
 156       if (java_lang_String::equals(string_object_no_keepalive(l), name, len)) {
 157         // We must perform a new load with string_object() that keeps the string
 158         // alive as we must expose the oop as strongly reachable when exiting
 159         // this context, in case the oop gets published.
 160         return string_object(l);
 161       }
 162     }
 163   }
 164   // If the bucket size is too deep check if this hash code is insufficient.
 165   if (count >= rehash_count && !needs_rehashing()) {
 166     _needs_rehashing = check_rehash_table(count);
 167   }
 168   return NULL;
 169 }
 170 
 171 
 172 oop StringTable::basic_add(int index_arg, Handle string, jchar* name,
 173                            int len, unsigned int hashValue_arg, TRAPS) {
 174 
 175   assert(java_lang_String::equals(string(), name, len),
 176          "string must be properly initialized");
 177   // Cannot hit a safepoint in this function because the "this" pointer can move.
 178   NoSafepointVerifier nsv;
 179 
 180   // Check if the symbol table has been rehashed, if so, need to recalculate
 181   // the hash value and index before second lookup.
 182   unsigned int hashValue;
 183   int index;
 184   if (use_alternate_hashcode()) {
 185     hashValue = alt_hash_string(name, len);
 186     index = hash_to_index(hashValue);
 187   } else {
 188     hashValue = hashValue_arg;
 189     index = index_arg;
 190   }
 191 
 192   // Since look-up was done lock-free, we need to check if another
 193   // thread beat us in the race to insert the symbol.
 194 
 195   // No need to lookup the shared table from here since the caller (intern()) already did
 196   oop test = lookup_in_main_table(index, name, len, hashValue); // calls lookup(u1*, int)
 197   if (test != NULL) {
 198     // Entry already added
 199     return test;
 200   }
 201 
 202   HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string());
 203   add_entry(index, entry);
 204   return string();
 205 }
 206 
 207 
 208 oop StringTable::lookup(Symbol* symbol) {
 209   ResourceMark rm;
 210   int length;
 211   jchar* chars = symbol->as_unicode(length);
 212   return lookup(chars, length);
 213 }
 214 
 215 oop StringTable::lookup(jchar* name, int len) {
 216   // shared table always uses java_lang_String::hash_code
 217   unsigned int hash = java_lang_String::hash_code(name, len);
 218   oop string = lookup_shared(name, len, hash);
 219   if (string != NULL) {
 220     return string;
 221   }
 222   if (use_alternate_hashcode()) {
 223     hash = alt_hash_string(name, len);
 224   }
 225   int index = the_table()->hash_to_index(hash);
 226   string = the_table()->lookup_in_main_table(index, name, len, hash);
 227 
 228   return string;
 229 }
 230 
 231 oop StringTable::intern(Handle string_or_null, jchar* name,
 232                         int len, TRAPS) {
 233   // shared table always uses java_lang_String::hash_code
 234   unsigned int hashValue = java_lang_String::hash_code(name, len);
 235   oop found_string = lookup_shared(name, len, hashValue);
 236   if (found_string != NULL) {
 237     return found_string;
 238   }
 239   if (use_alternate_hashcode()) {
 240     hashValue = alt_hash_string(name, len);
 241   }
 242   int index = the_table()->hash_to_index(hashValue);
 243   found_string = the_table()->lookup_in_main_table(index, name, len, hashValue);
 244 
 245   // Found
 246   if (found_string != NULL) {
 247     return found_string;
 248   }
 249 
 250   debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
 251   assert(!Universe::heap()->is_in_reserved(name),
 252          "proposed name of symbol must be stable");
 253 
 254   HandleMark hm(THREAD);  // cleanup strings created
 255   Handle string;
 256   // try to reuse the string if possible
 257   if (!string_or_null.is_null()) {
 258     string = string_or_null;
 259   } else {
 260     string = java_lang_String::create_from_unicode(name, len, CHECK_NULL);
 261   }
 262 
 263 #if INCLUDE_G1GC
 264   if (G1StringDedup::is_enabled()) {
 265     // Deduplicate the string before it is interned. Note that we should never
 266     // deduplicate a string after it has been interned. Doing so will counteract
 267     // compiler optimizations done on e.g. interned string literals.
 268     G1StringDedup::deduplicate(string());
 269   }
 270 #endif
 271 
 272   // Grab the StringTable_lock before getting the_table() because it could
 273   // change at safepoint.
 274   oop added_or_found;
 275   {
 276     MutexLocker ml(StringTable_lock, THREAD);
 277     // Otherwise, add to symbol to table
 278     added_or_found = the_table()->basic_add(index, string, name, len,
 279                                   hashValue, CHECK_NULL);
 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(string_object_no_keepalive(entry))) {
 391         if (f != NULL) {
 392           f->do_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 = string_object_no_keepalive(p);
 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()->print_table_statistics(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 = string_object_no_keepalive(p);
 451         typeArrayOop value     = java_lang_String::value_no_keepalive(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 = string_object_no_keepalive(e_ptr1);
 487   oop str2 = string_object_no_keepalive(e_ptr2);
 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 = string_object_no_keepalive(e_ptr);
 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 #if INCLUDE_CDS_JAVA_HEAP
 681 // Sharing
 682 oop StringTable::create_archived_string(oop s, Thread* THREAD) {
 683   assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
 684 
 685   oop new_s = NULL;
 686   typeArrayOop v = java_lang_String::value_no_keepalive(s);
 687   typeArrayOop new_v = (typeArrayOop)MetaspaceShared::archive_heap_object(v, THREAD);
 688   if (new_v == NULL) {
 689     return NULL;
 690   }
 691   new_s = MetaspaceShared::archive_heap_object(s, THREAD);
 692   if (new_s == NULL) {
 693     return NULL;
 694   }
 695 
 696   // adjust the pointer to the 'value' field in the new String oop
 697   java_lang_String::set_value_raw(new_s, new_v);
 698   return new_s;
 699 }
 700 
 701 bool StringTable::copy_shared_string(GrowableArray<MemRegion> *string_space,
 702                                      CompactStringTableWriter* writer) {
 703   assert(MetaspaceShared::is_heap_object_archiving_allowed(), "must be");
 704 
 705   Thread* THREAD = Thread::current();
 706   for (int i = 0; i < the_table()->table_size(); ++i) {
 707     HashtableEntry<oop, mtSymbol>* bucket = the_table()->bucket(i);
 708     for ( ; bucket != NULL; bucket = bucket->next()) {
 709       oop s = string_object_no_keepalive(bucket);
 710       unsigned int hash = java_lang_String::hash_code(s);
 711       if (hash == 0) {
 712         continue;
 713       }
 714 
 715       java_lang_String::set_hash(s, hash);
 716       oop new_s = create_archived_string(s, THREAD);
 717       if (new_s == NULL) {
 718         continue;
 719       }
 720 
 721       // set the archived string in bucket
 722       set_string_object(bucket, new_s);
 723 
 724       // add to the compact table
 725       writer->add(hash, new_s);
 726     }
 727   }
 728 
 729   return true;
 730 }
 731 
 732 void StringTable::write_to_archive(GrowableArray<MemRegion> *string_space) {
 733   assert(MetaspaceShared::is_heap_object_archiving_allowed(), "must be");
 734 
 735   _shared_table.reset();
 736   int num_buckets = the_table()->number_of_entries() /
 737                          SharedSymbolTableBucketSize;
 738   // calculation of num_buckets can result in zero buckets, we need at least one
 739   CompactStringTableWriter writer(num_buckets > 1 ? num_buckets : 1,
 740                                   &MetaspaceShared::stats()->string);
 741 
 742   // Copy the interned strings into the "string space" within the java heap
 743   if (copy_shared_string(string_space, &writer)) {
 744     writer.dump(&_shared_table);
 745   }
 746 }
 747 
 748 void StringTable::serialize(SerializeClosure* soc) {
 749   _shared_table.set_type(CompactHashtable<oop, char>::_string_table);
 750   _shared_table.serialize(soc);
 751 
 752   if (soc->writing()) {
 753     _shared_table.reset(); // Sanity. Make sure we don't use the shared table at dump time
 754   } else if (!_shared_string_mapped) {
 755     _shared_table.reset();
 756   }
 757 }
 758 
 759 void StringTable::shared_oops_do(OopClosure* f) {
 760   _shared_table.oops_do(f);
 761 }
 762 #endif //INCLUDE_CDS_JAVA_HEAP