1 /* 2 * Copyright (c) 1997, 2018, 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 char* Symbol::as_C_string_flexible_buffer(Thread* t, 135 char* buf, int size) const { 136 char* str; 137 int len = utf8_length(); 138 int buf_len = len + 1; 139 if (size < buf_len) { 140 str = NEW_RESOURCE_ARRAY(char, buf_len); 141 } else { 142 str = buf; 143 } 144 return as_C_string(str, buf_len); 145 } 146 147 void Symbol::print_utf8_on(outputStream* st) const { 148 st->print("%s", as_C_string()); 149 } 150 151 void Symbol::print_symbol_on(outputStream* st) const { 152 char *s; 153 st = st ? st : tty; 154 { 155 // ResourceMark may not affect st->print(). If st is a string 156 // stream it could resize, using the same resource arena. 157 ResourceMark rm; 158 s = as_quoted_ascii(); 159 s = os::strdup(s); 160 } 161 if (s == NULL) { 162 st->print("(null)"); 163 } else { 164 st->print("%s", s); 165 os::free(s); 166 } 167 } 168 169 char* Symbol::as_quoted_ascii() const { 170 const char *ptr = (const char *)&_body[0]; 171 int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length()); 172 char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1); 173 UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1); 174 return result; 175 } 176 177 jchar* Symbol::as_unicode(int& length) const { 178 Symbol* this_ptr = (Symbol*)this; 179 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length()); 180 jchar* result = NEW_RESOURCE_ARRAY(jchar, length); 181 if (length > 0) { 182 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length); 183 } 184 return result; 185 } 186 187 const char* Symbol::as_klass_external_name(char* buf, int size) const { 188 if (size > 0) { 189 char* str = as_C_string(buf, size); 190 int length = (int)strlen(str); 191 // Turn all '/'s into '.'s (also for array klasses) 192 for (int index = 0; index < length; index++) { 193 if (str[index] == '/') { 194 str[index] = '.'; 195 } 196 } 197 return str; 198 } else { 199 return buf; 200 } 201 } 202 203 const char* Symbol::as_klass_external_name() const { 204 char* str = as_C_string(); 205 int length = (int)strlen(str); 206 // Turn all '/'s into '.'s (also for array klasses) 207 for (int index = 0; index < length; index++) { 208 if (str[index] == '/') { 209 str[index] = '.'; 210 } 211 } 212 return str; 213 } 214 215 // Alternate hashing for unbalanced symbol tables. 216 unsigned int Symbol::new_hash(juint seed) { 217 ResourceMark rm; 218 // Use alternate hashing algorithm on this symbol. 219 return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length()); 220 } 221 222 // Increment refcount while checking for zero. If the Symbol's refcount becomes zero 223 // a thread could be concurrently removing the Symbol. This is used during SymbolTable 224 // lookup to avoid reviving a dead Symbol. 225 bool Symbol::try_increment_refcount() { 226 uint32_t found = _length_and_refcount; 227 while (true) { 228 uint32_t old_value = found; 229 int refc = extract_refcount(old_value); 230 if (refc == PERM_REFCOUNT) { 231 return true; // sticky max or created permanent 232 } else if (refc == 0) { 233 return false; // dead, can't revive. 234 } else { 235 found = Atomic::cmpxchg(old_value + 1, &_length_and_refcount, old_value); 236 if (found == old_value) { 237 return true; // successfully updated. 238 } 239 // refcount changed, try again. 240 } 241 } 242 } 243 244 // The increment_refcount() is called when not doing lookup. It is assumed that you 245 // have a symbol with a non-zero refcount and it can't become zero while referenced by 246 // this caller. 247 void Symbol::increment_refcount() { 248 if (!try_increment_refcount()) { 249 #ifdef ASSERT 250 print(); 251 fatal("refcount has gone to zero"); 252 #endif 253 } 254 #ifndef PRODUCT 255 if (refcount() != PERM_REFCOUNT) { // not a permanent symbol 256 NOT_PRODUCT(Atomic::inc(&_total_count);) 257 } 258 #endif 259 } 260 261 // Decrement refcount potentially while racing increment, so we need 262 // to check the value after attempting to decrement so that if another 263 // thread increments to PERM_REFCOUNT the value is not decremented. 264 void Symbol::decrement_refcount() { 265 uint32_t found = _length_and_refcount; 266 while (true) { 267 uint32_t old_value = found; 268 int refc = extract_refcount(old_value); 269 if (refc == PERM_REFCOUNT) { 270 return; // refcount is permanent, permanent is sticky 271 } else if (refc == 0) { 272 #ifdef ASSERT 273 print(); 274 fatal("refcount underflow"); 275 #endif 276 return; 277 } else { 278 found = Atomic::cmpxchg(old_value - 1, &_length_and_refcount, old_value); 279 if (found == old_value) { 280 return; // successfully updated. 281 } 282 // refcount changed, try again. 283 } 284 } 285 } 286 287 void Symbol::metaspace_pointers_do(MetaspaceClosure* it) { 288 if (log_is_enabled(Trace, cds)) { 289 LogStream trace_stream(Log(cds)::trace()); 290 trace_stream.print("Iter(Symbol): %p ", this); 291 print_value_on(&trace_stream); 292 trace_stream.cr(); 293 } 294 } 295 296 void Symbol::print_on(outputStream* st) const { 297 st->print("Symbol: '"); 298 print_symbol_on(st); 299 st->print("'"); 300 st->print(" count %d", refcount()); 301 } 302 303 // The print_value functions are present in all builds, to support the 304 // disassembler and error reporting. 305 void Symbol::print_value_on(outputStream* st) const { 306 st->print("'"); 307 for (int i = 0; i < utf8_length(); i++) { 308 st->print("%c", char_at(i)); 309 } 310 st->print("'"); 311 } 312 313 bool Symbol::is_valid(Symbol* s) { 314 if (!is_aligned(s, sizeof(MetaWord))) return false; 315 if ((size_t)s < os::min_page_size()) return false; 316 317 if (!os::is_readable_range(s, s + 1)) return false; 318 319 // Symbols are not allocated in Java heap. 320 if (Universe::heap()->is_in_reserved(s)) return false; 321 322 int len = s->utf8_length(); 323 if (len < 0) return false; 324 325 jbyte* bytes = (jbyte*) s->bytes(); 326 return os::is_readable_range(bytes, bytes + len); 327 } 328 329 // SymbolTable prints this in its statistics 330 NOT_PRODUCT(size_t Symbol::_total_count = 0;) --- EOF ---