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