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