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