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