1 /*
   2  * Copyright (c) 1997, 2017, 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 "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "oops/symbol.hpp"
  34 #include "runtime/atomic.hpp"
  35 #include "runtime/os.hpp"
  36 
  37 uint32_t Symbol::pack_length_and_refcount(int length, int refcount) {
  38   STATIC_ASSERT(max_symbol_length == ((1 << 16) - 1));
  39   STATIC_ASSERT(PERM_REFCOUNT == ((1 << 16) - 1));
  40   assert(length >= 0, "negative length");
  41   assert(length <= max_symbol_length, "too long symbol");
  42   assert(refcount >= 0, "negative refcount");
  43   assert(refcount <= PERM_REFCOUNT, "invalid refcount");
  44   uint32_t hi = length;
  45   uint32_t lo = refcount;
  46   return (hi << 16) | lo;
  47 }
  48 
  49 Symbol::Symbol(const u1* name, int length, int refcount) {
  50   _length_and_refcount =  pack_length_and_refcount(length, refcount);
  51   _identity_hash = (short)os::random();
  52   for (int i = 0; i < length; i++) {
  53     byte_at_put(i, name[i]);
  54   }
  55 }
  56 
  57 void* Symbol::operator new(size_t sz, int len, TRAPS) throw() {
  58   int alloc_size = size(len)*wordSize;
  59   address res = (address) AllocateHeap(alloc_size, mtSymbol);
  60   return res;
  61 }
  62 
  63 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() {
  64   int alloc_size = size(len)*wordSize;
  65   address res = (address)arena->Amalloc_4(alloc_size);
  66   return res;
  67 }
  68 
  69 void Symbol::operator delete(void *p) {
  70   assert(((Symbol*)p)->refcount() == 0, "should not call this");
  71   FreeHeap(p);
  72 }
  73 
  74 // ------------------------------------------------------------------
  75 // Symbol::starts_with
  76 //
  77 // Tests if the symbol starts with the specified prefix of the given
  78 // length.
  79 bool Symbol::starts_with(const char* prefix, int len) const {
  80   if (len > utf8_length()) return false;
  81   while (len-- > 0) {
  82     if (prefix[len] != (char) byte_at(len))
  83       return false;
  84   }
  85   assert(len == -1, "we should be at the beginning");
  86   return true;
  87 }
  88 
  89 
  90 // ------------------------------------------------------------------
  91 // Symbol::index_of
  92 //
  93 // Finds if the given string is a substring of this symbol's utf8 bytes.
  94 // Return -1 on failure.  Otherwise return the first index where str occurs.
  95 int Symbol::index_of_at(int i, const char* str, int len) const {
  96   assert(i >= 0 && i <= utf8_length(), "oob");
  97   if (len <= 0)  return 0;
  98   char first_char = str[0];
  99   address bytes = (address) ((Symbol*)this)->base();
 100   address limit = bytes + utf8_length() - len;  // inclusive limit
 101   address scan = bytes + i;
 102   if (scan > limit)
 103     return -1;
 104   for (; scan <= limit; scan++) {
 105     scan = (address) memchr(scan, first_char, (limit + 1 - scan));
 106     if (scan == NULL)
 107       return -1;  // not found
 108     assert(scan >= bytes+i && scan <= limit, "scan oob");
 109     if (memcmp(scan, str, len) == 0)
 110       return (int)(scan - bytes);
 111   }
 112   return -1;
 113 }
 114 
 115 
 116 char* Symbol::as_C_string(char* buf, int size) const {
 117   if (size > 0) {
 118     int len = MIN2(size - 1, utf8_length());
 119     for (int i = 0; i < len; i++) {
 120       buf[i] = byte_at(i);
 121     }
 122     buf[len] = '\0';
 123   }
 124   return buf;
 125 }
 126 
 127 char* Symbol::as_C_string() const {
 128   int len = utf8_length();
 129   char* str = NEW_RESOURCE_ARRAY(char, len + 1);
 130   return as_C_string(str, len + 1);
 131 }
 132 
 133 char* Symbol::as_C_string_flexible_buffer(Thread* t,
 134                                                  char* buf, int size) const {
 135   char* str;
 136   int len = utf8_length();
 137   int buf_len = len + 1;
 138   if (size < buf_len) {
 139     str = NEW_RESOURCE_ARRAY(char, buf_len);
 140   } else {
 141     str = buf;
 142   }
 143   return as_C_string(str, buf_len);
 144 }
 145 
 146 void Symbol::print_utf8_on(outputStream* st) const {
 147   st->print("%s", as_C_string());
 148 }
 149 
 150 void Symbol::print_symbol_on(outputStream* st) const {
 151   char *s;
 152   st = st ? st : tty;
 153   {
 154     // ResourceMark may not affect st->print(). If st is a string
 155     // stream it could resize, using the same resource arena.
 156     ResourceMark rm;
 157     s = as_quoted_ascii();
 158     s = os::strdup(s);
 159   }
 160   if (s == NULL) {
 161     st->print("(null)");
 162   } else {
 163     st->print("%s", s);
 164     os::free(s);
 165   }
 166 }
 167 
 168 char* Symbol::as_quoted_ascii() const {
 169   const char *ptr = (const char *)&_body[0];
 170   int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
 171   char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
 172   UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
 173   return result;
 174 }
 175 
 176 jchar* Symbol::as_unicode(int& length) const {
 177   Symbol* this_ptr = (Symbol*)this;
 178   length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
 179   jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
 180   if (length > 0) {
 181     UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
 182   }
 183   return result;
 184 }
 185 
 186 const char* Symbol::as_klass_external_name(char* buf, int size) const {
 187   if (size > 0) {
 188     char* str    = as_C_string(buf, size);
 189     int   length = (int)strlen(str);
 190     // Turn all '/'s into '.'s (also for array klasses)
 191     for (int index = 0; index < length; index++) {
 192       if (str[index] == '/') {
 193         str[index] = '.';
 194       }
 195     }
 196     return str;
 197   } else {
 198     return buf;
 199   }
 200 }
 201 
 202 const char* Symbol::as_klass_external_name() const {
 203   char* str    = as_C_string();
 204   int   length = (int)strlen(str);
 205   // Turn all '/'s into '.'s (also for array klasses)
 206   for (int index = 0; index < length; index++) {
 207     if (str[index] == '/') {
 208       str[index] = '.';
 209     }
 210   }
 211   return str;
 212 }
 213 
 214 // Alternate hashing for unbalanced symbol tables.
 215 unsigned int Symbol::new_hash(juint seed) {
 216   ResourceMark rm;
 217   // Use alternate hashing algorithm on this symbol.
 218   return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
 219 }
 220 
 221 void Symbol::increment_refcount() {
 222   if (refcount() != PERM_REFCOUNT) { // not a permanent symbol
 223     if (!try_increment_refcount()) {
 224 #ifdef ASSERT
 225       print();
 226 #endif
 227       fatal("refcount has gone to zero");
 228     }
 229     NOT_PRODUCT(Atomic::inc(&_total_count);)
 230   }
 231 }
 232 
 233 void Symbol::decrement_refcount() {
 234   if (refcount() != PERM_REFCOUNT) { // not a permanent symbol
 235     int new_value = Atomic::sub((uint32_t)1, &_length_and_refcount);
 236 #ifdef ASSERT
 237     // Check if we have transitioned to 0xffff
 238     if (extract_refcount(new_value) == PERM_REFCOUNT) {
 239       print();
 240       fatal("refcount underflow");
 241     }
 242 #endif
 243   }
 244 }
 245 
 246 // Atomically increment while checking for zero, zero is bad.
 247 bool Symbol::try_increment_refcount() {
 248   uint32_t old_value = _length_and_refcount;  // fetch once
 249   int refc = extract_refcount(old_value);
 250 
 251   if (refc == PERM_REFCOUNT) {
 252     return true;
 253   } else if (refc == 0) {
 254     return false; // effectively dead, can't revive
 255   }
 256 
 257   uint32_t now;
 258   while ((now = Atomic::cmpxchg(old_value + 1, &_length_and_refcount, old_value)) != old_value) {
 259     // failed to increment, check refcount again.
 260     refc = extract_refcount(now);
 261     if (refc == 0) {
 262       return false; // just died
 263     } else if (refc == PERM_REFCOUNT) {
 264       return true; // just became permanent
 265     }
 266     old_value = now; // refcount changed, try again
 267   }
 268   return true;
 269 }
 270 
 271 void Symbol::metaspace_pointers_do(MetaspaceClosure* it) {
 272   if (log_is_enabled(Trace, cds)) {
 273     LogStream trace_stream(Log(cds)::trace());
 274     trace_stream.print("Iter(Symbol): %p ", this);
 275     print_value_on(&trace_stream);
 276     trace_stream.cr();
 277   }
 278 }
 279 
 280 void Symbol::print_on(outputStream* st) const {
 281   if (this == NULL) {
 282     st->print_cr("NULL");
 283   } else {
 284     st->print("Symbol: '");
 285     print_symbol_on(st);
 286     st->print("'");
 287     st->print(" count %d", refcount());
 288   }
 289 }
 290 
 291 // The print_value functions are present in all builds, to support the
 292 // disassembler and error reporting.
 293 void Symbol::print_value_on(outputStream* st) const {
 294   if (this == NULL) {
 295     st->print("NULL");
 296   } else {
 297     st->print("'");
 298     for (int i = 0; i < utf8_length(); i++) {
 299       st->print("%c", byte_at(i));
 300     }
 301     st->print("'");
 302   }
 303 }
 304 
 305 // SymbolTable prints this in its statistics
 306 NOT_PRODUCT(int Symbol::_total_count = 0;)