1 /* 2 * Copyright (c) 1997, 2021, 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 "cds/metaspaceShared.hpp" 28 #include "classfile/altHashing.hpp" 29 #include "classfile/classLoaderData.hpp" 30 #include "classfile/vmSymbols.hpp" 31 #include "gc/shared/collectedHeap.hpp" 32 #include "logging/log.hpp" 33 #include "logging/logStream.hpp" 34 #include "memory/allocation.inline.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "memory/universe.hpp" 37 #include "oops/symbol.hpp" 38 #include "runtime/atomic.hpp" 39 #include "runtime/mutexLocker.hpp" 40 #include "runtime/os.hpp" 41 #include "runtime/signature.hpp" 42 #include "utilities/utf8.hpp" 43 44 Symbol* Symbol::_vm_symbols[vmSymbols::number_of_symbols()]; 45 46 uint32_t Symbol::pack_hash_and_refcount(short hash, int refcount) { 47 STATIC_ASSERT(PERM_REFCOUNT == ((1 << 16) - 1)); 48 assert(refcount >= 0, "negative refcount"); 49 assert(refcount <= PERM_REFCOUNT, "invalid refcount"); 50 uint32_t hi = hash; 51 uint32_t lo = refcount; 52 return (hi << 16) | lo; 53 } 54 55 Symbol::Symbol(const u1* name, int length, int refcount) { 56 _hash_and_refcount = pack_hash_and_refcount((short)os::random(), refcount); 57 _length = length; 58 // _body[0..1] are allocated in the header just by coincidence in the current 59 // implementation of Symbol. They are read by identity_hash(), so make sure they 60 // are initialized. 61 // No other code should assume that _body[0..1] are always allocated. E.g., do 62 // not unconditionally read base()[0] as that will be invalid for an empty Symbol. 63 _body[0] = _body[1] = 0; 64 memcpy(_body, name, length); 65 } 66 67 void* Symbol::operator new(size_t sz, int len) throw() { 68 #if INCLUDE_CDS 69 if (DumpSharedSpaces) { 70 MutexLocker ml(DumpRegion_lock, Mutex::_no_safepoint_check_flag); 71 // To get deterministic output from -Xshare:dump, we ensure that Symbols are allocated in 72 // increasing addresses. When the symbols are copied into the archive, we preserve their 73 // relative address order (sorted, see ArchiveBuilder::gather_klasses_and_symbols). 74 // 75 // We cannot use arena because arena chunks are allocated by the OS. As a result, for example, 76 // the archived symbol of "java/lang/Object" may sometimes be lower than "java/lang/String", and 77 // sometimes be higher. This would cause non-deterministic contents in the archive. 78 DEBUG_ONLY(static void* last = 0); 79 void* p = (void*)MetaspaceShared::symbol_space_alloc(size(len)*wordSize); 80 assert(p > last, "must increase monotonically"); 81 DEBUG_ONLY(last = p); 82 return p; 83 } 84 #endif 85 int alloc_size = size(len)*wordSize; 86 address res = (address) AllocateHeap(alloc_size, mtSymbol); 87 return res; 88 } 89 90 void* Symbol::operator new(size_t sz, int len, Arena* arena) throw() { 91 int alloc_size = size(len)*wordSize; 92 address res = (address)arena->AmallocWords(alloc_size); 93 return res; 94 } 95 96 void Symbol::operator delete(void *p) { 97 assert(((Symbol*)p)->refcount() == 0, "should not call this"); 98 FreeHeap(p); 99 } 100 101 #if INCLUDE_CDS 102 void Symbol::update_identity_hash() { 103 // This is called at a safepoint during dumping of a static CDS archive. The caller should have 104 // called os::init_random() with a deterministic seed and then iterate all archived Symbols in 105 // a deterministic order. 106 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 107 _hash_and_refcount = pack_hash_and_refcount((short)os::random(), PERM_REFCOUNT); 108 } 109 110 void Symbol::set_permanent() { 111 // This is called at a safepoint during dumping of a dynamic CDS archive. 112 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 113 _hash_and_refcount = pack_hash_and_refcount(extract_hash(_hash_and_refcount), PERM_REFCOUNT); 114 } 115 #endif 116 117 bool Symbol::is_Q_signature() const { 118 int len = utf8_length(); 119 return len > 2 && char_at(0) == JVM_SIGNATURE_PRIMITIVE_OBJECT && char_at(len - 1) == JVM_SIGNATURE_ENDCLASS; 120 } 121 122 bool Symbol::is_Q_array_signature() const { 123 int l = utf8_length(); 124 if (l < 2 || char_at(0) != JVM_SIGNATURE_ARRAY || char_at(l - 1) != JVM_SIGNATURE_ENDCLASS) { 125 return false; 126 } 127 for (int i = 1; i < (l - 2); i++) { 128 char c = char_at(i); 129 if (c == JVM_SIGNATURE_PRIMITIVE_OBJECT) { 130 return true; 131 } 132 if (c != JVM_SIGNATURE_ARRAY) { 133 return false; 134 } 135 } 136 return false; 137 } 138 139 bool Symbol::is_Q_method_signature() const { 140 assert(SignatureVerifier::is_valid_method_signature(this), "must be"); 141 int len = utf8_length(); 142 if (len > 4 && char_at(0) == JVM_SIGNATURE_FUNC) { 143 for (int i=1; i<len-3; i++) { // Must end with ")Qx;", where x is at least one character or more. 144 if (char_at(i) == JVM_SIGNATURE_ENDFUNC && char_at(i+1) == JVM_SIGNATURE_PRIMITIVE_OBJECT) { 145 return true; 146 } 147 } 148 } 149 return false; 150 } 151 152 Symbol* Symbol::fundamental_name(TRAPS) { 153 if ((char_at(0) == JVM_SIGNATURE_PRIMITIVE_OBJECT || char_at(0) == JVM_SIGNATURE_CLASS) && ends_with(JVM_SIGNATURE_ENDCLASS)) { 154 return SymbolTable::new_symbol(this, 1, utf8_length() - 1); 155 } else { 156 // reference count is incremented to be consistent with the behavior with 157 // the SymbolTable::new_symbol() call above 158 this->increment_refcount(); 159 return this; 160 } 161 } 162 163 bool Symbol::is_same_fundamental_type(Symbol* s) const { 164 if (this == s) return true; 165 if (utf8_length() < 3) return false; 166 int offset1, offset2, len; 167 if (ends_with(JVM_SIGNATURE_ENDCLASS)) { 168 if (char_at(0) != JVM_SIGNATURE_PRIMITIVE_OBJECT && char_at(0) != JVM_SIGNATURE_CLASS) return false; 169 offset1 = 1; 170 len = utf8_length() - 2; 171 } else { 172 offset1 = 0; 173 len = utf8_length(); 174 } 175 if (ends_with(JVM_SIGNATURE_ENDCLASS)) { 176 if (s->char_at(0) != JVM_SIGNATURE_PRIMITIVE_OBJECT && s->char_at(0) != JVM_SIGNATURE_CLASS) return false; 177 offset2 = 1; 178 } else { 179 offset2 = 0; 180 } 181 if ((offset2 + len) > s->utf8_length()) return false; 182 if ((utf8_length() - offset1 * 2) != (s->utf8_length() - offset2 * 2)) 183 return false; 184 int l = len; 185 while (l-- > 0) { 186 if (char_at(offset1 + l) != s->char_at(offset2 + l)) 187 return false; 188 } 189 return true; 190 } 191 192 // ------------------------------------------------------------------ 193 // Symbol::index_of 194 // 195 // Finds if the given string is a substring of this symbol's utf8 bytes. 196 // Return -1 on failure. Otherwise return the first index where str occurs. 197 int Symbol::index_of_at(int i, const char* str, int len) const { 198 assert(i >= 0 && i <= utf8_length(), "oob"); 199 if (len <= 0) return 0; 200 char first_char = str[0]; 201 address bytes = (address) ((Symbol*)this)->base(); 202 address limit = bytes + utf8_length() - len; // inclusive limit 203 address scan = bytes + i; 204 if (scan > limit) 205 return -1; 206 for (; scan <= limit; scan++) { 207 scan = (address) memchr(scan, first_char, (limit + 1 - scan)); 208 if (scan == NULL) 209 return -1; // not found 210 assert(scan >= bytes+i && scan <= limit, "scan oob"); 211 if (len <= 2 212 ? (char) scan[len-1] == str[len-1] 213 : memcmp(scan+1, str+1, len-1) == 0) { 214 return (int)(scan - bytes); 215 } 216 } 217 return -1; 218 } 219 220 221 char* Symbol::as_C_string(char* buf, int size) const { 222 if (size > 0) { 223 int len = MIN2(size - 1, utf8_length()); 224 for (int i = 0; i < len; i++) { 225 buf[i] = char_at(i); 226 } 227 buf[len] = '\0'; 228 } 229 return buf; 230 } 231 232 char* Symbol::as_C_string() const { 233 int len = utf8_length(); 234 char* str = NEW_RESOURCE_ARRAY(char, len + 1); 235 return as_C_string(str, len + 1); 236 } 237 238 void Symbol::print_utf8_on(outputStream* st) const { 239 st->print("%s", as_C_string()); 240 } 241 242 void Symbol::print_symbol_on(outputStream* st) const { 243 char *s; 244 st = st ? st : tty; 245 { 246 // ResourceMark may not affect st->print(). If st is a string 247 // stream it could resize, using the same resource arena. 248 ResourceMark rm; 249 s = as_quoted_ascii(); 250 s = os::strdup(s); 251 } 252 if (s == NULL) { 253 st->print("(null)"); 254 } else { 255 st->print("%s", s); 256 os::free(s); 257 } 258 } 259 260 char* Symbol::as_quoted_ascii() const { 261 const char *ptr = (const char *)&_body[0]; 262 int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length()); 263 char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1); 264 UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1); 265 return result; 266 } 267 268 jchar* Symbol::as_unicode(int& length) const { 269 Symbol* this_ptr = (Symbol*)this; 270 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length()); 271 jchar* result = NEW_RESOURCE_ARRAY(jchar, length); 272 if (length > 0) { 273 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length); 274 } 275 return result; 276 } 277 278 const char* Symbol::as_klass_external_name(char* buf, int size) const { 279 if (size > 0) { 280 char* str = as_C_string(buf, size); 281 int length = (int)strlen(str); 282 // Turn all '/'s into '.'s (also for array klasses) 283 for (int index = 0; index < length; index++) { 284 if (str[index] == JVM_SIGNATURE_SLASH) { 285 str[index] = JVM_SIGNATURE_DOT; 286 } 287 } 288 return str; 289 } else { 290 return buf; 291 } 292 } 293 294 const char* Symbol::as_klass_external_name() const { 295 char* str = as_C_string(); 296 int length = (int)strlen(str); 297 // Turn all '/'s into '.'s (also for array klasses) 298 for (int index = 0; index < length; index++) { 299 if (str[index] == JVM_SIGNATURE_SLASH) { 300 str[index] = JVM_SIGNATURE_DOT; 301 } 302 } 303 return str; 304 } 305 306 static void print_class(outputStream *os, const SignatureStream& ss) { 307 int sb = ss.raw_symbol_begin(), se = ss.raw_symbol_end(); 308 for (int i = sb; i < se; ++i) { 309 int ch = ss.raw_char_at(i); 310 if (ch == JVM_SIGNATURE_SLASH) { 311 os->put(JVM_SIGNATURE_DOT); 312 } else { 313 os->put(ch); 314 } 315 } 316 } 317 318 static void print_array(outputStream *os, SignatureStream& ss) { 319 int dimensions = ss.skip_array_prefix(); 320 assert(dimensions > 0, ""); 321 if (ss.is_reference()) { 322 print_class(os, ss); 323 } else { 324 os->print("%s", type2name(ss.type())); 325 } 326 for (int i = 0; i < dimensions; ++i) { 327 os->print("[]"); 328 } 329 } 330 331 void Symbol::print_as_signature_external_return_type(outputStream *os) { 332 for (SignatureStream ss(this); !ss.is_done(); ss.next()) { 333 if (ss.at_return_type()) { 334 if (ss.is_array()) { 335 print_array(os, ss); 336 } else if (ss.is_reference()) { 337 print_class(os, ss); 338 } else { 339 os->print("%s", type2name(ss.type())); 340 } 341 } 342 } 343 } 344 345 void Symbol::print_as_signature_external_parameters(outputStream *os) { 346 bool first = true; 347 for (SignatureStream ss(this); !ss.is_done(); ss.next()) { 348 if (ss.at_return_type()) break; 349 if (!first) { os->print(", "); } 350 if (ss.is_array()) { 351 print_array(os, ss); 352 } else if (ss.is_reference()) { 353 print_class(os, ss); 354 } else { 355 os->print("%s", type2name(ss.type())); 356 } 357 first = false; 358 } 359 } 360 361 // Increment refcount while checking for zero. If the Symbol's refcount becomes zero 362 // a thread could be concurrently removing the Symbol. This is used during SymbolTable 363 // lookup to avoid reviving a dead Symbol. 364 bool Symbol::try_increment_refcount() { 365 uint32_t found = _hash_and_refcount; 366 while (true) { 367 uint32_t old_value = found; 368 int refc = extract_refcount(old_value); 369 if (refc == PERM_REFCOUNT) { 370 return true; // sticky max or created permanent 371 } else if (refc == 0) { 372 return false; // dead, can't revive. 373 } else { 374 found = Atomic::cmpxchg(&_hash_and_refcount, old_value, old_value + 1); 375 if (found == old_value) { 376 return true; // successfully updated. 377 } 378 // refcount changed, try again. 379 } 380 } 381 } 382 383 // The increment_refcount() is called when not doing lookup. It is assumed that you 384 // have a symbol with a non-zero refcount and it can't become zero while referenced by 385 // this caller. 386 void Symbol::increment_refcount() { 387 if (!try_increment_refcount()) { 388 #ifdef ASSERT 389 print(); 390 fatal("refcount has gone to zero"); 391 #endif 392 } 393 #ifndef PRODUCT 394 if (refcount() != PERM_REFCOUNT) { // not a permanent symbol 395 NOT_PRODUCT(Atomic::inc(&_total_count);) 396 } 397 #endif 398 } 399 400 // Decrement refcount potentially while racing increment, so we need 401 // to check the value after attempting to decrement so that if another 402 // thread increments to PERM_REFCOUNT the value is not decremented. 403 void Symbol::decrement_refcount() { 404 uint32_t found = _hash_and_refcount; 405 while (true) { 406 uint32_t old_value = found; 407 int refc = extract_refcount(old_value); 408 if (refc == PERM_REFCOUNT) { 409 return; // refcount is permanent, permanent is sticky 410 } else if (refc == 0) { 411 #ifdef ASSERT 412 print(); 413 fatal("refcount underflow"); 414 #endif 415 return; 416 } else { 417 found = Atomic::cmpxchg(&_hash_and_refcount, old_value, old_value - 1); 418 if (found == old_value) { 419 return; // successfully updated. 420 } 421 // refcount changed, try again. 422 } 423 } 424 } 425 426 void Symbol::make_permanent() { 427 uint32_t found = _hash_and_refcount; 428 while (true) { 429 uint32_t old_value = found; 430 int refc = extract_refcount(old_value); 431 if (refc == PERM_REFCOUNT) { 432 return; // refcount is permanent, permanent is sticky 433 } else if (refc == 0) { 434 #ifdef ASSERT 435 print(); 436 fatal("refcount underflow"); 437 #endif 438 return; 439 } else { 440 int hash = extract_hash(old_value); 441 found = Atomic::cmpxchg(&_hash_and_refcount, old_value, pack_hash_and_refcount(hash, PERM_REFCOUNT)); 442 if (found == old_value) { 443 return; // successfully updated. 444 } 445 // refcount changed, try again. 446 } 447 } 448 } 449 450 void Symbol::metaspace_pointers_do(MetaspaceClosure* it) { 451 if (log_is_enabled(Trace, cds)) { 452 LogStream trace_stream(Log(cds)::trace()); 453 trace_stream.print("Iter(Symbol): %p ", this); 454 print_value_on(&trace_stream); 455 trace_stream.cr(); 456 } 457 } 458 459 void Symbol::print_on(outputStream* st) const { 460 st->print("Symbol: '"); 461 print_symbol_on(st); 462 st->print("'"); 463 st->print(" count %d", refcount()); 464 } 465 466 void Symbol::print() const { print_on(tty); } 467 468 // The print_value functions are present in all builds, to support the 469 // disassembler and error reporting. 470 void Symbol::print_value_on(outputStream* st) const { 471 st->print("'"); 472 for (int i = 0; i < utf8_length(); i++) { 473 st->print("%c", char_at(i)); 474 } 475 st->print("'"); 476 } 477 478 void Symbol::print_value() const { print_value_on(tty); } 479 480 bool Symbol::is_valid(Symbol* s) { 481 if (!is_aligned(s, sizeof(MetaWord))) return false; 482 if ((size_t)s < os::min_page_size()) return false; 483 484 if (!os::is_readable_range(s, s + 1)) return false; 485 486 // Symbols are not allocated in Java heap. 487 if (Universe::heap()->is_in(s)) return false; 488 489 int len = s->utf8_length(); 490 if (len < 0) return false; 491 492 jbyte* bytes = (jbyte*) s->bytes(); 493 return os::is_readable_range(bytes, bytes + len); 494 } 495 496 void Symbol::print_Qvalue_on(outputStream* st) const { 497 if (this == NULL) { 498 st->print("NULL"); 499 } else { 500 st->print("'Q"); 501 for (int i = 0; i < utf8_length(); i++) { 502 st->print("%c", char_at(i)); 503 } 504 st->print(";'"); 505 } 506 } 507 508 // SymbolTable prints this in its statistics 509 NOT_PRODUCT(size_t Symbol::_total_count = 0;) 510 511 #ifndef PRODUCT 512 bool Symbol::is_valid_id(vmSymbolID vm_symbol_id) { 513 return vmSymbols::is_valid_id(vm_symbol_id); 514 } 515 #endif