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