1 /*
   2  * Copyright (c) 1997, 2019, 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 
  26 #include "precompiled.hpp"
  27 #include "classfile/altHashing.hpp"
  28 #include "classfile/classLoaderData.hpp"
  29 #include "gc/shared/collectedHeap.hpp"
  30 #include "logging/log.hpp"
  31 #include "logging/logStream.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/symbol.hpp"
  35 #include "runtime/atomic.hpp"
  36 #include "runtime/os.hpp"
  37 #include "utilities/utf8.hpp"
  38 
  39 uint32_t Symbol::pack_length_and_refcount(int length, int refcount) {
  40   STATIC_ASSERT(max_symbol_length == ((1 << 16) - 1));
  41   STATIC_ASSERT(PERM_REFCOUNT == ((1 << 16) - 1));
  42   assert(length >= 0, "negative length");
  43   assert(length <= max_symbol_length, "too long symbol");
  44   assert(refcount >= 0, "negative refcount");
  45   assert(refcount <= PERM_REFCOUNT, "invalid refcount");
  46   uint32_t hi = length;
  47   uint32_t lo = refcount;
  48   return (hi << 16) | lo;
  49 }
  50 
  51 Symbol::Symbol(const u1* name, int length, int refcount) {
  52   _length_and_refcount =  pack_length_and_refcount(length, refcount);
  53   _identity_hash = (short)os::random();
  54   for (int i = 0; i < length; i++) {
  55     byte_at_put(i, name[i]);
  56   }
  57 }
  58 
  59 void* Symbol::operator new(size_t sz, int len, TRAPS) throw() {
  60   int alloc_size = size(len)*wordSize;
  61   address res = (address) AllocateHeap(alloc_size, mtSymbol);
  62   return res;
  63 }
  64 
  65 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() {
  66   int alloc_size = size(len)*wordSize;
  67   address res = (address)arena->Amalloc_4(alloc_size);
  68   return res;
  69 }
  70 
  71 void Symbol::operator delete(void *p) {
  72   assert(((Symbol*)p)->refcount() == 0, "should not call this");
  73   FreeHeap(p);
  74 }
  75 
  76 // ------------------------------------------------------------------
  77 // Symbol::contains_byte_at
  78 //
  79 // Tests if the symbol contains the given byte at the given position.
  80 bool Symbol::contains_byte_at(int position, char code_byte) const {
  81   if (position < 0)  return false;  // can happen with ends_with
  82   if (position >= utf8_length()) return false;
  83   return code_byte == char_at(position);
  84 }
  85 
  86 // ------------------------------------------------------------------
  87 // Symbol::contains_utf8_at
  88 //
  89 // Tests if the symbol contains the given utf8 substring
  90 // at the given byte position.
  91 bool Symbol::contains_utf8_at(int position, const char* substring, int len) const {
  92   assert(len > 0 && substring != NULL && (int) strlen(substring) >= len,
  93          "substring must be valid");
  94   if (len == 1)  return contains_byte_at(position, substring[0]);
  95   if (position < 0)  return false;  // can happen with ends_with
  96   if (position + len > utf8_length()) return false;
  97   while (len-- > 0) {
  98     if (substring[len] != char_at(position + len))
  99       return false;
 100   }
 101   assert(len == -1, "we should be at the beginning");
 102   return true;
 103 }
 104 
 105 bool Symbol::is_Q_signature() const {
 106   return utf8_length() > 2 && char_at(0) == 'Q' && ends_with(';');
 107 }
 108 
 109 Symbol* Symbol::fundamental_name(TRAPS) {
 110   if ((char_at(0) == 'Q' || char_at(0) == 'L') && ends_with(';')) {
 111     return SymbolTable::lookup(this, 1, utf8_length() - 1, CHECK_NULL);
 112   } else {
 113     // reference count is incremented to be consistent with the behavior with
 114     // the SymbolTable::lookup() call above
 115     this->increment_refcount();
 116     return this;
 117   }
 118 }
 119 
 120 bool Symbol::is_same_fundamental_type(Symbol* s) const {
 121   if (this == s) return true;
 122   if (utf8_length() < 3) return false;
 123   int offset1, offset2, len;
 124   if (ends_with(';')) {
 125     if (char_at(0) != 'Q' && char_at(0) != 'L') return false;
 126     offset1 = 1;
 127     len = utf8_length() - 2;
 128   } else {
 129     offset1 = 0;
 130     len = utf8_length();
 131   }
 132   if (ends_with(';')) {
 133     if (s->char_at(0) != 'Q' && s->char_at(0) != 'L') return false;
 134     offset2 = 1;
 135   } else {
 136     offset2 = 0;
 137   }
 138   if ((offset2 + len) > s->utf8_length()) return false;
 139   if ((utf8_length() - offset1 * 2) != (s->utf8_length() - offset2 * 2))
 140     return false;
 141   int l = len;
 142   while (l-- > 0) {
 143     if (char_at(offset1 + l) != s->char_at(offset2 + l))
 144       return false;
 145   }
 146   return true;
 147 }
 148 
 149 // ------------------------------------------------------------------
 150 // Symbol::index_of
 151 //
 152 // Finds if the given string is a substring of this symbol's utf8 bytes.
 153 // Return -1 on failure.  Otherwise return the first index where str occurs.
 154 int Symbol::index_of_at(int i, const char* str, int len) const {
 155   assert(i >= 0 && i <= utf8_length(), "oob");
 156   if (len <= 0)  return 0;
 157   char first_char = str[0];
 158   address bytes = (address) ((Symbol*)this)->base();
 159   address limit = bytes + utf8_length() - len;  // inclusive limit
 160   address scan = bytes + i;
 161   if (scan > limit)
 162     return -1;
 163   for (; scan <= limit; scan++) {
 164     scan = (address) memchr(scan, first_char, (limit + 1 - scan));
 165     if (scan == NULL)
 166       return -1;  // not found
 167     assert(scan >= bytes+i && scan <= limit, "scan oob");
 168     if (memcmp(scan, str, len) == 0)
 169       return (int)(scan - bytes);
 170   }
 171   return -1;
 172 }
 173 
 174 
 175 char* Symbol::as_C_string(char* buf, int size) const {
 176   if (size > 0) {
 177     int len = MIN2(size - 1, utf8_length());
 178     for (int i = 0; i < len; i++) {
 179       buf[i] = char_at(i);
 180     }
 181     buf[len] = '\0';
 182   }
 183   return buf;
 184 }
 185 
 186 char* Symbol::as_C_string() const {
 187   int len = utf8_length();
 188   char* str = NEW_RESOURCE_ARRAY(char, len + 1);
 189   return as_C_string(str, len + 1);
 190 }
 191 
 192 void Symbol::print_utf8_on(outputStream* st) const {
 193   st->print("%s", as_C_string());
 194 }
 195 
 196 void Symbol::print_symbol_on(outputStream* st) const {
 197   char *s;
 198   st = st ? st : tty;
 199   {
 200     // ResourceMark may not affect st->print(). If st is a string
 201     // stream it could resize, using the same resource arena.
 202     ResourceMark rm;
 203     s = as_quoted_ascii();
 204     s = os::strdup(s);
 205   }
 206   if (s == NULL) {
 207     st->print("(null)");
 208   } else {
 209     st->print("%s", s);
 210     os::free(s);
 211   }
 212 }
 213 
 214 char* Symbol::as_quoted_ascii() const {
 215   const char *ptr = (const char *)&_body[0];
 216   int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
 217   char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
 218   UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
 219   return result;
 220 }
 221 
 222 jchar* Symbol::as_unicode(int& length) const {
 223   Symbol* this_ptr = (Symbol*)this;
 224   length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
 225   jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
 226   if (length > 0) {
 227     UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
 228   }
 229   return result;
 230 }
 231 
 232 const char* Symbol::as_klass_external_name(char* buf, int size) const {
 233   if (size > 0) {
 234     char* str    = as_C_string(buf, size);
 235     int   length = (int)strlen(str);
 236     // Turn all '/'s into '.'s (also for array klasses)
 237     for (int index = 0; index < length; index++) {
 238       if (str[index] == '/') {
 239         str[index] = '.';
 240       }
 241     }
 242     return str;
 243   } else {
 244     return buf;
 245   }
 246 }
 247 
 248 const char* Symbol::as_klass_external_name() const {
 249   char* str    = as_C_string();
 250   int   length = (int)strlen(str);
 251   // Turn all '/'s into '.'s (also for array klasses)
 252   for (int index = 0; index < length; index++) {
 253     if (str[index] == '/') {
 254       str[index] = '.';
 255     }
 256   }
 257   return str;
 258 }
 259 
 260 static void print_class(outputStream *os, char *class_str, int len) {
 261   for (int i = 0; i < len; ++i) {
 262     if (class_str[i] == '/') {
 263       os->put('.');
 264     } else {
 265       os->put(class_str[i]);
 266     }
 267   }
 268 }
 269 
 270 static void print_array(outputStream *os, char *array_str, int len) {
 271   int dimensions = 0;
 272   for (int i = 0; i < len; ++i) {
 273     if (array_str[i] == '[') {
 274       dimensions++;
 275     } else if (array_str[i] == 'L' || array_str[i] == 'Q') {
 276       // Expected format: L<type name>;. Skip 'L' and ';' delimiting the type name.
 277       print_class(os, array_str+i+1, len-i-2);
 278       break;
 279     } else {
 280       os->print("%s", type2name(char2type(array_str[i])));
 281     }
 282   }
 283   for (int i = 0; i < dimensions; ++i) {
 284     os->print("[]");
 285   }
 286 }
 287 
 288 void Symbol::print_as_signature_external_return_type(outputStream *os) {
 289   for (SignatureStream ss(this); !ss.is_done(); ss.next()) {
 290     if (ss.at_return_type()) {
 291       if (ss.is_array()) {
 292         print_array(os, (char*)ss.raw_bytes(), (int)ss.raw_length());
 293       } else if (ss.is_object()) {
 294         // Expected format: L<type name>;. Skip 'L' and ';' delimiting the class name.
 295         print_class(os, (char*)ss.raw_bytes()+1, (int)ss.raw_length()-2);
 296       } else {
 297         os->print("%s", type2name(ss.type()));
 298       }
 299     }
 300   }
 301 }
 302 
 303 void Symbol::print_as_signature_external_parameters(outputStream *os) {
 304   bool first = true;
 305   for (SignatureStream ss(this); !ss.is_done(); ss.next()) {
 306     if (ss.at_return_type()) break;
 307     if (!first) { os->print(", "); }
 308     if (ss.is_array()) {
 309       print_array(os, (char*)ss.raw_bytes(), (int)ss.raw_length());
 310     } else if (ss.is_object()) {
 311       // Skip 'L' and ';'.
 312       print_class(os, (char*)ss.raw_bytes()+1, (int)ss.raw_length()-2);
 313     } else {
 314       os->print("%s", type2name(ss.type()));
 315     }
 316     first = false;
 317   }
 318 }
 319 
 320 // Increment refcount while checking for zero.  If the Symbol's refcount becomes zero
 321 // a thread could be concurrently removing the Symbol.  This is used during SymbolTable
 322 // lookup to avoid reviving a dead Symbol.
 323 bool Symbol::try_increment_refcount() {
 324   uint32_t found = _length_and_refcount;
 325   while (true) {
 326     uint32_t old_value = found;
 327     int refc = extract_refcount(old_value);
 328     if (refc == PERM_REFCOUNT) {
 329       return true;  // sticky max or created permanent
 330     } else if (refc == 0) {
 331       return false; // dead, can't revive.
 332     } else {
 333       found = Atomic::cmpxchg(old_value + 1, &_length_and_refcount, old_value);
 334       if (found == old_value) {
 335         return true; // successfully updated.
 336       }
 337       // refcount changed, try again.
 338     }
 339   }
 340 }
 341 
 342 // The increment_refcount() is called when not doing lookup. It is assumed that you
 343 // have a symbol with a non-zero refcount and it can't become zero while referenced by
 344 // this caller.
 345 void Symbol::increment_refcount() {
 346   if (!try_increment_refcount()) {
 347 #ifdef ASSERT
 348     print();
 349     fatal("refcount has gone to zero");
 350 #endif
 351   }
 352 #ifndef PRODUCT
 353   if (refcount() != PERM_REFCOUNT) { // not a permanent symbol
 354     NOT_PRODUCT(Atomic::inc(&_total_count);)
 355   }
 356 #endif
 357 }
 358 
 359 // Decrement refcount potentially while racing increment, so we need
 360 // to check the value after attempting to decrement so that if another
 361 // thread increments to PERM_REFCOUNT the value is not decremented.
 362 void Symbol::decrement_refcount() {
 363   uint32_t found = _length_and_refcount;
 364   while (true) {
 365     uint32_t old_value = found;
 366     int refc = extract_refcount(old_value);
 367     if (refc == PERM_REFCOUNT) {
 368       return;  // refcount is permanent, permanent is sticky
 369     } else if (refc == 0) {
 370 #ifdef ASSERT
 371       print();
 372       fatal("refcount underflow");
 373 #endif
 374       return;
 375     } else {
 376       found = Atomic::cmpxchg(old_value - 1, &_length_and_refcount, old_value);
 377       if (found == old_value) {
 378         return;  // successfully updated.
 379       }
 380       // refcount changed, try again.
 381     }
 382   }
 383 }
 384 
 385 void Symbol::make_permanent() {
 386   uint32_t found = _length_and_refcount;
 387   while (true) {
 388     uint32_t old_value = found;
 389     int refc = extract_refcount(old_value);
 390     if (refc == PERM_REFCOUNT) {
 391       return;  // refcount is permanent, permanent is sticky
 392     } else if (refc == 0) {
 393 #ifdef ASSERT
 394       print();
 395       fatal("refcount underflow");
 396 #endif
 397       return;
 398     } else {
 399       int len = extract_length(old_value);
 400       found = Atomic::cmpxchg(pack_length_and_refcount(len, PERM_REFCOUNT), &_length_and_refcount, old_value);
 401       if (found == old_value) {
 402         return;  // successfully updated.
 403       }
 404       // refcount changed, try again.
 405     }
 406   }
 407 }
 408 
 409 void Symbol::metaspace_pointers_do(MetaspaceClosure* it) {
 410   if (log_is_enabled(Trace, cds)) {
 411     LogStream trace_stream(Log(cds)::trace());
 412     trace_stream.print("Iter(Symbol): %p ", this);
 413     print_value_on(&trace_stream);
 414     trace_stream.cr();
 415   }
 416 }
 417 
 418 void Symbol::print_on(outputStream* st) const {
 419   st->print("Symbol: '");
 420   print_symbol_on(st);
 421   st->print("'");
 422   st->print(" count %d", refcount());
 423 }
 424 
 425 // The print_value functions are present in all builds, to support the
 426 // disassembler and error reporting.
 427 void Symbol::print_value_on(outputStream* st) const {
 428   st->print("'");
 429   for (int i = 0; i < utf8_length(); i++) {
 430     st->print("%c", char_at(i));
 431   }
 432   st->print("'");
 433 }
 434 
 435 bool Symbol::is_valid(Symbol* s) {
 436   if (!is_aligned(s, sizeof(MetaWord))) return false;
 437   if ((size_t)s < os::min_page_size()) return false;
 438 
 439   if (!os::is_readable_range(s, s + 1)) return false;
 440 
 441   // Symbols are not allocated in Java heap.
 442   if (Universe::heap()->is_in_reserved(s)) return false;
 443 
 444   int len = s->utf8_length();
 445   if (len < 0) return false;
 446 
 447   jbyte* bytes = (jbyte*) s->bytes();
 448   return os::is_readable_range(bytes, bytes + len);
 449 }
 450 
 451 void Symbol::print_Qvalue_on(outputStream* st) const {
 452   if (this == NULL) {
 453     st->print("NULL");
 454   } else {
 455     st->print("'Q");
 456     for (int i = 0; i < utf8_length(); i++) {
 457       st->print("%c", char_at(i));
 458     }
 459     st->print(";'");
 460   }
 461 }
 462 
 463 // SymbolTable prints this in its statistics
 464 NOT_PRODUCT(size_t Symbol::_total_count = 0;)