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