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 bool Symbol::is_Q_signature() const {
 82   int len = utf8_length();
 83   return len > 2 && char_at(0) == JVM_SIGNATURE_VALUETYPE && char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
 84 }
 85 
 86 bool Symbol::is_L_signature() const {
 87   int len = utf8_length();
 88   return len > 2 && char_at(0) == JVM_SIGNATURE_CLASS && char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
 89 }
 90 
 91 bool Symbol::is_Q_array_signature() const {
 92   int l = utf8_length();
 93   if (l < 2 || char_at(0) != JVM_SIGNATURE_ARRAY || char_at(l - 1) != JVM_SIGNATURE_ENDCLASS) {
 94     return false;
 95   }
 96   for (int i = 1; i < (l - 2); i++) {
 97     char c = char_at(i);
 98     if (c == JVM_SIGNATURE_VALUETYPE) {
 99       return true;
100     }
101     if (c != JVM_SIGNATURE_ARRAY) {
102       return false;
103     }
104   }
105   return false;
106 }
107 
108 bool Symbol::is_Q_method_signature() const {
109   assert(SignatureVerifier::is_valid_method_signature(this), "must be");
110   int len = utf8_length();
111   if (len > 4 && char_at(0) == JVM_SIGNATURE_FUNC) {
112     for (int i=1; i<len-3; i++) { // Must end with ")Qx;", where x is at least one character or more.
113       if (char_at(i) == JVM_SIGNATURE_ENDFUNC && char_at(i+1) == JVM_SIGNATURE_VALUETYPE) {
114         return true;
115       }
116     }
117   }
118   return false;
119 }
120 
121 bool Symbol::is_Q_singledim_array_signature() const {
122   int len = utf8_length();
123   return len > 3 && char_at(0) == JVM_SIGNATURE_ARRAY && char_at(1) == JVM_SIGNATURE_VALUETYPE &&
124                     char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
125 }
126 
127 Symbol* Symbol::fundamental_name(TRAPS) {
128   if ((char_at(0) == JVM_SIGNATURE_VALUETYPE || char_at(0) == JVM_SIGNATURE_CLASS) && ends_with(JVM_SIGNATURE_ENDCLASS)) {
129     return SymbolTable::new_symbol(this, 1, utf8_length() - 1);
130   } else {
131     // reference count is incremented to be consistent with the behavior with
132     // the SymbolTable::new_symbol() call above
133     this->increment_refcount();
134     return this;
135   }
136 }
137 
138 bool Symbol::is_same_fundamental_type(Symbol* s) const {
139   if (this == s) return true;
140   if (utf8_length() < 3) return false;
141   int offset1, offset2, len;
142   if (ends_with(JVM_SIGNATURE_ENDCLASS)) {
143     if (char_at(0) != JVM_SIGNATURE_VALUETYPE && char_at(0) != JVM_SIGNATURE_CLASS) return false;
144     offset1 = 1;
145     len = utf8_length() - 2;
146   } else {
147     offset1 = 0;
148     len = utf8_length();
149   }
150   if (ends_with(JVM_SIGNATURE_ENDCLASS)) {
151     if (s->char_at(0) != JVM_SIGNATURE_VALUETYPE && s->char_at(0) != JVM_SIGNATURE_CLASS) return false;
152     offset2 = 1;
153   } else {
154     offset2 = 0;
155   }
156   if ((offset2 + len) > s->utf8_length()) return false;
157   if ((utf8_length() - offset1 * 2) != (s->utf8_length() - offset2 * 2))
158     return false;
159   int l = len;
160   while (l-- > 0) {
161     if (char_at(offset1 + l) != s->char_at(offset2 + l))
162       return false;
163   }
164   return true;
165 }
166 
167 // ------------------------------------------------------------------
168 // Symbol::index_of
169 //
170 // Finds if the given string is a substring of this symbol's utf8 bytes.
171 // Return -1 on failure.  Otherwise return the first index where str occurs.
172 int Symbol::index_of_at(int i, const char* str, int len) const {
173   assert(i >= 0 && i <= utf8_length(), "oob");
174   if (len <= 0)  return 0;
175   char first_char = str[0];
176   address bytes = (address) ((Symbol*)this)->base();
177   address limit = bytes + utf8_length() - len;  // inclusive limit
178   address scan = bytes + i;
179   if (scan > limit)
180     return -1;
181   for (; scan <= limit; scan++) {
182     scan = (address) memchr(scan, first_char, (limit + 1 - scan));
183     if (scan == NULL)
184       return -1;  // not found
185     assert(scan >= bytes+i && scan <= limit, "scan oob");
186     if (len <= 2
187         ? (char) scan[len-1] == str[len-1]
188         : memcmp(scan+1, str+1, len-1) == 0) {
189       return (int)(scan - bytes);
190     }
191   }
192   return -1;
193 }
194 
195 
196 char* Symbol::as_C_string(char* buf, int size) const {
197   if (size > 0) {
198     int len = MIN2(size - 1, utf8_length());
199     for (int i = 0; i < len; i++) {
200       buf[i] = char_at(i);
201     }
202     buf[len] = '\0';
203   }
204   return buf;
205 }
206 
207 char* Symbol::as_C_string() const {
208   int len = utf8_length();
209   char* str = NEW_RESOURCE_ARRAY(char, len + 1);
210   return as_C_string(str, len + 1);
211 }
212 
213 void Symbol::print_utf8_on(outputStream* st) const {
214   st->print("%s", as_C_string());
215 }
216 
217 void Symbol::print_symbol_on(outputStream* st) const {
218   char *s;
219   st = st ? st : tty;
220   {
221     // ResourceMark may not affect st->print(). If st is a string
222     // stream it could resize, using the same resource arena.
223     ResourceMark rm;
224     s = as_quoted_ascii();
225     s = os::strdup(s);
226   }
227   if (s == NULL) {
228     st->print("(null)");
229   } else {
230     st->print("%s", s);
231     os::free(s);
232   }
233 }
234 
235 char* Symbol::as_quoted_ascii() const {
236   const char *ptr = (const char *)&_body[0];
237   int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
238   char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
239   UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
240   return result;
241 }
242 
243 jchar* Symbol::as_unicode(int& length) const {
244   Symbol* this_ptr = (Symbol*)this;
245   length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
246   jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
247   if (length > 0) {
248     UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
249   }
250   return result;
251 }
252 
253 const char* Symbol::as_klass_external_name(char* buf, int size) const {
254   if (size > 0) {
255     char* str    = as_C_string(buf, size);
256     int   length = (int)strlen(str);
257     // Turn all '/'s into '.'s (also for array klasses)
258     for (int index = 0; index < length; index++) {
259       if (str[index] == JVM_SIGNATURE_SLASH) {
260         str[index] = JVM_SIGNATURE_DOT;
261       }
262     }
263     return str;
264   } else {
265     return buf;
266   }
267 }
268 
269 const char* Symbol::as_klass_external_name() const {
270   char* str    = as_C_string();
271   int   length = (int)strlen(str);
272   // Turn all '/'s into '.'s (also for array klasses)
273   for (int index = 0; index < length; index++) {
274     if (str[index] == JVM_SIGNATURE_SLASH) {
275       str[index] = JVM_SIGNATURE_DOT;
276     }
277   }
278   return str;
279 }
280 
281 static void print_class(outputStream *os, const SignatureStream& ss) {
282   int sb = ss.raw_symbol_begin(), se = ss.raw_symbol_end();
283   for (int i = sb; i < se; ++i) {
284     int ch = ss.raw_char_at(i);
285     if (ch == JVM_SIGNATURE_SLASH) {
286       os->put(JVM_SIGNATURE_DOT);
287     } else {
288       os->put(ch);
289     }
290   }
291 }
292 
293 static void print_array(outputStream *os, SignatureStream& ss) {
294   int dimensions = ss.skip_array_prefix();
295   assert(dimensions > 0, "");
296   if (ss.is_reference()) {
297     print_class(os, ss);
298   } else {
299     os->print("%s", type2name(ss.type()));
300   }
301   for (int i = 0; i < dimensions; ++i) {
302     os->print("[]");
303   }
304 }
305 
306 void Symbol::print_as_signature_external_return_type(outputStream *os) {
307   for (SignatureStream ss(this); !ss.is_done(); ss.next()) {
308     if (ss.at_return_type()) {
309       if (ss.is_array()) {
310         print_array(os, ss);
311       } else if (ss.is_reference()) {
312         print_class(os, ss);
313       } else {
314         os->print("%s", type2name(ss.type()));
315       }
316     }
317   }
318 }
319 
320 void Symbol::print_as_signature_external_parameters(outputStream *os) {
321   bool first = true;
322   for (SignatureStream ss(this); !ss.is_done(); ss.next()) {
323     if (ss.at_return_type()) break;
324     if (!first) { os->print(", "); }
325     if (ss.is_array()) {
326       print_array(os, ss);
327     } else if (ss.is_reference()) {
328       print_class(os, ss);
329     } else {
330       os->print("%s", type2name(ss.type()));
331     }
332     first = false;
333   }
334 }
335 
336 // Increment refcount while checking for zero.  If the Symbol's refcount becomes zero
337 // a thread could be concurrently removing the Symbol.  This is used during SymbolTable
338 // lookup to avoid reviving a dead Symbol.
339 bool Symbol::try_increment_refcount() {
340   uint32_t found = _hash_and_refcount;
341   while (true) {
342     uint32_t old_value = found;
343     int refc = extract_refcount(old_value);
344     if (refc == PERM_REFCOUNT) {
345       return true;  // sticky max or created permanent
346     } else if (refc == 0) {
347       return false; // dead, can't revive.
348     } else {
349       found = Atomic::cmpxchg(&_hash_and_refcount, old_value, old_value + 1);
350       if (found == old_value) {
351         return true; // successfully updated.
352       }
353       // refcount changed, try again.
354     }
355   }
356 }
357 
358 // The increment_refcount() is called when not doing lookup. It is assumed that you
359 // have a symbol with a non-zero refcount and it can't become zero while referenced by
360 // this caller.
361 void Symbol::increment_refcount() {
362   if (!try_increment_refcount()) {
363 #ifdef ASSERT
364     print();
365     fatal("refcount has gone to zero");
366 #endif
367   }
368 #ifndef PRODUCT
369   if (refcount() != PERM_REFCOUNT) { // not a permanent symbol
370     NOT_PRODUCT(Atomic::inc(&_total_count);)
371   }
372 #endif
373 }
374 
375 // Decrement refcount potentially while racing increment, so we need
376 // to check the value after attempting to decrement so that if another
377 // thread increments to PERM_REFCOUNT the value is not decremented.
378 void Symbol::decrement_refcount() {
379   uint32_t found = _hash_and_refcount;
380   while (true) {
381     uint32_t old_value = found;
382     int refc = extract_refcount(old_value);
383     if (refc == PERM_REFCOUNT) {
384       return;  // refcount is permanent, permanent is sticky
385     } else if (refc == 0) {
386 #ifdef ASSERT
387       print();
388       fatal("refcount underflow");
389 #endif
390       return;
391     } else {
392       found = Atomic::cmpxchg(&_hash_and_refcount, old_value, old_value - 1);
393       if (found == old_value) {
394         return;  // successfully updated.
395       }
396       // refcount changed, try again.
397     }
398   }
399 }
400 
401 void Symbol::make_permanent() {
402   uint32_t found = _hash_and_refcount;
403   while (true) {
404     uint32_t old_value = found;
405     int refc = extract_refcount(old_value);
406     if (refc == PERM_REFCOUNT) {
407       return;  // refcount is permanent, permanent is sticky
408     } else if (refc == 0) {
409 #ifdef ASSERT
410       print();
411       fatal("refcount underflow");
412 #endif
413       return;
414     } else {
415       int hash = extract_hash(old_value);
416       found = Atomic::cmpxchg(&_hash_and_refcount, old_value, pack_hash_and_refcount(hash, PERM_REFCOUNT));
417       if (found == old_value) {
418         return;  // successfully updated.
419       }
420       // refcount changed, try again.
421     }
422   }
423 }
424 
425 void Symbol::metaspace_pointers_do(MetaspaceClosure* it) {
426   if (log_is_enabled(Trace, cds)) {
427     LogStream trace_stream(Log(cds)::trace());
428     trace_stream.print("Iter(Symbol): %p ", this);
429     print_value_on(&trace_stream);
430     trace_stream.cr();
431   }
432 }
433 
434 void Symbol::print_on(outputStream* st) const {
435   st->print("Symbol: '");
436   print_symbol_on(st);
437   st->print("'");
438   st->print(" count %d", refcount());
439 }
440 
441 void Symbol::print() const { print_on(tty); }
442 
443 // The print_value functions are present in all builds, to support the
444 // disassembler and error reporting.
445 void Symbol::print_value_on(outputStream* st) const {
446   st->print("'");
447   for (int i = 0; i < utf8_length(); i++) {
448     st->print("%c", char_at(i));
449   }
450   st->print("'");
451 }
452 
453 void Symbol::print_value() const { print_value_on(tty); }
454 
455 bool Symbol::is_valid(Symbol* s) {
456   if (!is_aligned(s, sizeof(MetaWord))) return false;
457   if ((size_t)s < os::min_page_size()) return false;
458 
459   if (!os::is_readable_range(s, s + 1)) return false;
460 
461   // Symbols are not allocated in Java heap.
462   if (Universe::heap()->is_in(s)) return false;
463 
464   int len = s->utf8_length();
465   if (len < 0) return false;
466 
467   jbyte* bytes = (jbyte*) s->bytes();
468   return os::is_readable_range(bytes, bytes + len);
469 }
470 
471 void Symbol::print_Qvalue_on(outputStream* st) const {
472   if (this == NULL) {
473     st->print("NULL");
474   } else {
475     st->print("'Q");
476     for (int i = 0; i < utf8_length(); i++) {
477       st->print("%c", char_at(i));
478     }
479     st->print(";'");
480   }
481 }
482 
483 // SymbolTable prints this in its statistics
484 NOT_PRODUCT(size_t Symbol::_total_count = 0;)