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