1 /*
   2  * Copyright (c) 1997, 2014, 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/javaClasses.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "gc_interface/collectedHeap.inline.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/filemap.hpp"
  33 #include "memory/gcLocker.inline.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "oops/oop.inline2.hpp"
  36 #include "runtime/atomic.inline.hpp"
  37 #include "runtime/mutexLocker.hpp"
  38 #include "utilities/hashtable.inline.hpp"
  39 #if INCLUDE_ALL_GCS
  40 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  41 #include "gc_implementation/g1/g1StringDedup.hpp"
  42 #endif
  43 
  44 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  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 
  90 bool StringTable::_needs_rehashing = false;
  91 
  92 volatile int StringTable::_parallel_claimed_idx = 0;
  93 
  94 // Pick hashing algorithm
  95 unsigned int StringTable::hash_string(const jchar* s, int len) {
  96   return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) :
  97                                     java_lang_String::hash_code(s, len);
  98 }
  99 
 100 oop StringTable::lookup(int index, jchar* name,
 101                         int len, unsigned int hash) {
 102   int count = 0;
 103   for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) {
 104     count++;
 105     if (l->hash() == hash) {
 106       if (java_lang_String::equals(l->literal(), name, len)) {
 107         return l->literal();
 108       }
 109     }
 110   }
 111   // If the bucket size is too deep check if this hash code is insufficient.
 112   if (count >= rehash_count && !needs_rehashing()) {
 113     _needs_rehashing = check_rehash_table(count);
 114   }
 115   return NULL;
 116 }
 117 
 118 
 119 oop StringTable::basic_add(int index_arg, Handle string, jchar* name,
 120                            int len, unsigned int hashValue_arg, TRAPS) {
 121 
 122   assert(java_lang_String::equals(string(), name, len),
 123          "string must be properly initialized");
 124   // Cannot hit a safepoint in this function because the "this" pointer can move.
 125   No_Safepoint_Verifier nsv;
 126 
 127   // Check if the symbol table has been rehashed, if so, need to recalculate
 128   // the hash value and index before second lookup.
 129   unsigned int hashValue;
 130   int index;
 131   if (use_alternate_hashcode()) {
 132     hashValue = hash_string(name, len);
 133     index = hash_to_index(hashValue);
 134   } else {
 135     hashValue = hashValue_arg;
 136     index = index_arg;
 137   }
 138 
 139   // Since look-up was done lock-free, we need to check if another
 140   // thread beat us in the race to insert the symbol.
 141 
 142   oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int)
 143   if (test != NULL) {
 144     // Entry already added
 145     return test;
 146   }
 147 
 148   HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string());
 149   add_entry(index, entry);
 150   return string();
 151 }
 152 
 153 
 154 oop StringTable::lookup(Symbol* symbol) {
 155   ResourceMark rm;
 156   int length;
 157   jchar* chars = symbol->as_unicode(length);
 158   return lookup(chars, length);
 159 }
 160 
 161 // Tell the GC that this string was looked up in the StringTable.
 162 static void ensure_string_alive(oop string) {
 163   // A lookup in the StringTable could return an object that was previously
 164   // considered dead. The SATB part of G1 needs to get notified about this
 165   // potential resurrection, otherwise the marking might not find the object.
 166 #if INCLUDE_ALL_GCS
 167   if (UseG1GC && string != NULL) {
 168     G1SATBCardTableModRefBS::enqueue(string);
 169   }
 170 #endif
 171 }
 172 
 173 oop StringTable::lookup(jchar* name, int len) {
 174   unsigned int hash = hash_string(name, len);
 175   int index = the_table()->hash_to_index(hash);
 176   oop string = the_table()->lookup(index, name, len, hash);
 177 
 178   ensure_string_alive(string);
 179 
 180   return string;
 181 }
 182 
 183 
 184 oop StringTable::intern(Handle string_or_null, jchar* name,
 185                         int len, TRAPS) {
 186   unsigned int hashValue = hash_string(name, len);
 187   int index = the_table()->hash_to_index(hashValue);
 188   oop found_string = the_table()->lookup(index, name, len, hashValue);
 189 
 190   // Found
 191   if (found_string != NULL) {
 192     ensure_string_alive(found_string);
 193     return found_string;
 194   }
 195 
 196   debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
 197   assert(!Universe::heap()->is_in_reserved(name),
 198          "proposed name of symbol must be stable");
 199 
 200   Handle string;
 201   // try to reuse the string if possible
 202   if (!string_or_null.is_null()) {
 203     string = string_or_null;
 204   } else {
 205     string = java_lang_String::create_from_unicode(name, len, CHECK_NULL);
 206   }
 207 
 208 #if INCLUDE_ALL_GCS
 209   if (G1StringDedup::is_enabled()) {
 210     // Deduplicate the string before it is interned. Note that we should never
 211     // deduplicate a string after it has been interned. Doing so will counteract
 212     // compiler optimizations done on e.g. interned string literals.
 213     G1StringDedup::deduplicate(string());
 214   }
 215 #endif
 216 
 217   // Grab the StringTable_lock before getting the_table() because it could
 218   // change at safepoint.
 219   oop added_or_found;
 220   {
 221     MutexLocker ml(StringTable_lock, THREAD);
 222     // Otherwise, add to symbol to table
 223     added_or_found = the_table()->basic_add(index, string, name, len,
 224                                   hashValue, CHECK_NULL);
 225   }
 226 
 227   ensure_string_alive(added_or_found);
 228 
 229   return added_or_found;
 230 }
 231 
 232 oop StringTable::intern(Symbol* symbol, TRAPS) {
 233   if (symbol == NULL) return NULL;
 234   ResourceMark rm(THREAD);
 235   int length;
 236   jchar* chars = symbol->as_unicode(length);
 237   Handle string;
 238   oop result = intern(string, chars, length, CHECK_NULL);
 239   return result;
 240 }
 241 
 242 
 243 oop StringTable::intern(oop string, TRAPS)
 244 {
 245   if (string == NULL) return NULL;
 246   ResourceMark rm(THREAD);
 247   int length;
 248   Handle h_string (THREAD, string);
 249   jchar* chars = java_lang_String::as_unicode_string(string, length, CHECK_NULL);
 250   oop result = intern(h_string, chars, length, CHECK_NULL);
 251   return result;
 252 }
 253 
 254 
 255 oop StringTable::intern(const char* utf8_string, TRAPS) {
 256   if (utf8_string == NULL) return NULL;
 257   ResourceMark rm(THREAD);
 258   int length = UTF8::unicode_length(utf8_string);
 259   jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
 260   UTF8::convert_to_unicode(utf8_string, chars, length);
 261   Handle string;
 262   oop result = intern(string, chars, length, CHECK_NULL);
 263   return result;
 264 }
 265 
 266 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) {
 267   buckets_unlink_or_oops_do(is_alive, f, 0, the_table()->table_size(), processed, removed);
 268 }
 269 
 270 void StringTable::possibly_parallel_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) {
 271   // Readers of the table are unlocked, so we should only be removing
 272   // entries at a safepoint.
 273   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 274   const int limit = the_table()->table_size();
 275 
 276   for (;;) {
 277     // Grab next set of buckets to scan
 278     int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize;
 279     if (start_idx >= limit) {
 280       // End of table
 281       break;
 282     }
 283 
 284     int end_idx = MIN2(limit, start_idx + ClaimChunkSize);
 285     buckets_unlink_or_oops_do(is_alive, f, start_idx, end_idx, processed, removed);
 286   }
 287 }
 288 
 289 void StringTable::buckets_oops_do(OopClosure* f, int start_idx, int end_idx) {
 290   const int limit = the_table()->table_size();
 291 
 292   assert(0 <= start_idx && start_idx <= limit,
 293          err_msg("start_idx (%d) is out of bounds", start_idx));
 294   assert(0 <= end_idx && end_idx <= limit,
 295          err_msg("end_idx (%d) is out of bounds", end_idx));
 296   assert(start_idx <= end_idx,
 297          err_msg("Index ordering: start_idx=%d, end_idx=%d",
 298                  start_idx, end_idx));
 299 
 300   for (int i = start_idx; i < end_idx; i += 1) {
 301     HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i);
 302     while (entry != NULL) {
 303       assert(!entry->is_shared(), "CDS not used for the StringTable");
 304 
 305       f->do_oop((oop*)entry->literal_addr());
 306 
 307       entry = entry->next();
 308     }
 309   }
 310 }
 311 
 312 void StringTable::buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, int* processed, int* removed) {
 313   const int limit = the_table()->table_size();
 314 
 315   assert(0 <= start_idx && start_idx <= limit,
 316          err_msg("start_idx (%d) is out of bounds", start_idx));
 317   assert(0 <= end_idx && end_idx <= limit,
 318          err_msg("end_idx (%d) is out of bounds", end_idx));
 319   assert(start_idx <= end_idx,
 320          err_msg("Index ordering: start_idx=%d, end_idx=%d",
 321                  start_idx, end_idx));
 322 
 323   for (int i = start_idx; i < end_idx; ++i) {
 324     HashtableEntry<oop, mtSymbol>** p = the_table()->bucket_addr(i);
 325     HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i);
 326     while (entry != NULL) {
 327       assert(!entry->is_shared(), "CDS not used for the StringTable");
 328 
 329       if (is_alive->do_object_b(entry->literal())) {
 330         if (f != NULL) {
 331           f->do_oop((oop*)entry->literal_addr());
 332         }
 333         p = entry->next_addr();
 334       } else {
 335         *p = entry->next();
 336         the_table()->free_entry(entry);
 337         (*removed)++;
 338       }
 339       (*processed)++;
 340       entry = *p;
 341     }
 342   }
 343 }
 344 
 345 void StringTable::oops_do(OopClosure* f) {
 346   buckets_oops_do(f, 0, the_table()->table_size());
 347 }
 348 
 349 void StringTable::possibly_parallel_oops_do(OopClosure* f) {
 350   const int limit = the_table()->table_size();
 351 
 352   for (;;) {
 353     // Grab next set of buckets to scan
 354     int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize;
 355     if (start_idx >= limit) {
 356       // End of table
 357       break;
 358     }
 359 
 360     int end_idx = MIN2(limit, start_idx + ClaimChunkSize);
 361     buckets_oops_do(f, start_idx, end_idx);
 362   }
 363 }
 364 
 365 // This verification is part of Universe::verify() and needs to be quick.
 366 // See StringTable::verify_and_compare() below for exhaustive verification.
 367 void StringTable::verify() {
 368   for (int i = 0; i < the_table()->table_size(); ++i) {
 369     HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i);
 370     for ( ; p != NULL; p = p->next()) {
 371       oop s = p->literal();
 372       guarantee(s != NULL, "interned string is NULL");
 373       unsigned int h = java_lang_String::hash_string(s);
 374       guarantee(p->hash() == h, "broken hash in string table entry");
 375       guarantee(the_table()->hash_to_index(h) == i,
 376                 "wrong index in string table");
 377     }
 378   }
 379 }
 380 
 381 void StringTable::dump(outputStream* st) {
 382   the_table()->dump_table(st, "StringTable");
 383 }
 384 
 385 StringTable::VerifyRetTypes StringTable::compare_entries(
 386                                       int bkt1, int e_cnt1,
 387                                       HashtableEntry<oop, mtSymbol>* e_ptr1,
 388                                       int bkt2, int e_cnt2,
 389                                       HashtableEntry<oop, mtSymbol>* e_ptr2) {
 390   // These entries are sanity checked by verify_and_compare_entries()
 391   // before this function is called.
 392   oop str1 = e_ptr1->literal();
 393   oop str2 = e_ptr2->literal();
 394 
 395   if (str1 == str2) {
 396     tty->print_cr("ERROR: identical oop values (0x" PTR_FORMAT ") "
 397                   "in entry @ bucket[%d][%d] and entry @ bucket[%d][%d]",
 398                   (void *)str1, bkt1, e_cnt1, bkt2, e_cnt2);
 399     return _verify_fail_continue;
 400   }
 401 
 402   if (java_lang_String::equals(str1, str2)) {
 403     tty->print_cr("ERROR: identical String values in entry @ "
 404                   "bucket[%d][%d] and entry @ bucket[%d][%d]",
 405                   bkt1, e_cnt1, bkt2, e_cnt2);
 406     return _verify_fail_continue;
 407   }
 408 
 409   return _verify_pass;
 410 }
 411 
 412 StringTable::VerifyRetTypes StringTable::verify_entry(int bkt, int e_cnt,
 413                                       HashtableEntry<oop, mtSymbol>* e_ptr,
 414                                       StringTable::VerifyMesgModes mesg_mode) {
 415 
 416   VerifyRetTypes ret = _verify_pass;  // be optimistic
 417 
 418   oop str = e_ptr->literal();
 419   if (str == NULL) {
 420     if (mesg_mode == _verify_with_mesgs) {
 421       tty->print_cr("ERROR: NULL oop value in entry @ bucket[%d][%d]", bkt,
 422                     e_cnt);
 423     }
 424     // NULL oop means no more verifications are possible
 425     return _verify_fail_done;
 426   }
 427 
 428   if (str->klass() != SystemDictionary::String_klass()) {
 429     if (mesg_mode == _verify_with_mesgs) {
 430       tty->print_cr("ERROR: oop is not a String in entry @ bucket[%d][%d]",
 431                     bkt, e_cnt);
 432     }
 433     // not a String means no more verifications are possible
 434     return _verify_fail_done;
 435   }
 436 
 437   unsigned int h = java_lang_String::hash_string(str);
 438   if (e_ptr->hash() != h) {
 439     if (mesg_mode == _verify_with_mesgs) {
 440       tty->print_cr("ERROR: broken hash value in entry @ bucket[%d][%d], "
 441                     "bkt_hash=%d, str_hash=%d", bkt, e_cnt, e_ptr->hash(), h);
 442     }
 443     ret = _verify_fail_continue;
 444   }
 445 
 446   if (the_table()->hash_to_index(h) != bkt) {
 447     if (mesg_mode == _verify_with_mesgs) {
 448       tty->print_cr("ERROR: wrong index value for entry @ bucket[%d][%d], "
 449                     "str_hash=%d, hash_to_index=%d", bkt, e_cnt, h,
 450                     the_table()->hash_to_index(h));
 451     }
 452     ret = _verify_fail_continue;
 453   }
 454 
 455   return ret;
 456 }
 457 
 458 // See StringTable::verify() above for the quick verification that is
 459 // part of Universe::verify(). This verification is exhaustive and
 460 // reports on every issue that is found. StringTable::verify() only
 461 // reports on the first issue that is found.
 462 //
 463 // StringTable::verify_entry() checks:
 464 // - oop value != NULL (same as verify())
 465 // - oop value is a String
 466 // - hash(String) == hash in entry (same as verify())
 467 // - index for hash == index of entry (same as verify())
 468 //
 469 // StringTable::compare_entries() checks:
 470 // - oops are unique across all entries
 471 // - String values are unique across all entries
 472 //
 473 int StringTable::verify_and_compare_entries() {
 474   assert(StringTable_lock->is_locked(), "sanity check");
 475 
 476   int  fail_cnt = 0;
 477 
 478   // first, verify all the entries individually:
 479   for (int bkt = 0; bkt < the_table()->table_size(); bkt++) {
 480     HashtableEntry<oop, mtSymbol>* e_ptr = the_table()->bucket(bkt);
 481     for (int e_cnt = 0; e_ptr != NULL; e_ptr = e_ptr->next(), e_cnt++) {
 482       VerifyRetTypes ret = verify_entry(bkt, e_cnt, e_ptr, _verify_with_mesgs);
 483       if (ret != _verify_pass) {
 484         fail_cnt++;
 485       }
 486     }
 487   }
 488 
 489   // Optimization: if the above check did not find any failures, then
 490   // the comparison loop below does not need to call verify_entry()
 491   // before calling compare_entries(). If there were failures, then we
 492   // have to call verify_entry() to see if the entry can be passed to
 493   // compare_entries() safely. When we call verify_entry() in the loop
 494   // below, we do so quietly to void duplicate messages and we don't
 495   // increment fail_cnt because the failures have already been counted.
 496   bool need_entry_verify = (fail_cnt != 0);
 497 
 498   // second, verify all entries relative to each other:
 499   for (int bkt1 = 0; bkt1 < the_table()->table_size(); bkt1++) {
 500     HashtableEntry<oop, mtSymbol>* e_ptr1 = the_table()->bucket(bkt1);
 501     for (int e_cnt1 = 0; e_ptr1 != NULL; e_ptr1 = e_ptr1->next(), e_cnt1++) {
 502       if (need_entry_verify) {
 503         VerifyRetTypes ret = verify_entry(bkt1, e_cnt1, e_ptr1,
 504                                           _verify_quietly);
 505         if (ret == _verify_fail_done) {
 506           // cannot use the current entry to compare against other entries
 507           continue;
 508         }
 509       }
 510 
 511       for (int bkt2 = bkt1; bkt2 < the_table()->table_size(); bkt2++) {
 512         HashtableEntry<oop, mtSymbol>* e_ptr2 = the_table()->bucket(bkt2);
 513         int e_cnt2;
 514         for (e_cnt2 = 0; e_ptr2 != NULL; e_ptr2 = e_ptr2->next(), e_cnt2++) {
 515           if (bkt1 == bkt2 && e_cnt2 <= e_cnt1) {
 516             // skip the entries up to and including the one that
 517             // we're comparing against
 518             continue;
 519           }
 520 
 521           if (need_entry_verify) {
 522             VerifyRetTypes ret = verify_entry(bkt2, e_cnt2, e_ptr2,
 523                                               _verify_quietly);
 524             if (ret == _verify_fail_done) {
 525               // cannot compare against this entry
 526               continue;
 527             }
 528           }
 529 
 530           // compare two entries, report and count any failures:
 531           if (compare_entries(bkt1, e_cnt1, e_ptr1, bkt2, e_cnt2, e_ptr2)
 532               != _verify_pass) {
 533             fail_cnt++;
 534           }
 535         }
 536       }
 537     }
 538   }
 539   return fail_cnt;
 540 }
 541 
 542 // Create a new table and using alternate hash code, populate the new table
 543 // with the existing strings.   Set flag to use the alternate hash code afterwards.
 544 void StringTable::rehash_table() {
 545   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 546   // This should never happen with -Xshare:dump but it might in testing mode.
 547   if (DumpSharedSpaces) return;
 548   StringTable* new_table = new StringTable();
 549 
 550   // Rehash the table
 551   the_table()->move_to(new_table);
 552 
 553   // Delete the table and buckets (entries are reused in new table).
 554   delete _the_table;
 555   // Don't check if we need rehashing until the table gets unbalanced again.
 556   // Then rehash with a new global seed.
 557   _needs_rehashing = false;
 558   _the_table = new_table;
 559 }