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