1 /*
  2  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_RUNTIME_SIGNATURE_HPP
 27 #define SHARE_RUNTIME_SIGNATURE_HPP
 28 
 29 #include "classfile/symbolTable.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "oops/method.hpp"
 32 #include "sanitizers/ub.hpp"
 33 
 34 
 35 // Static routines and parsing loops for processing field and method
 36 // descriptors.  In the HotSpot sources we call them "signatures".
 37 //
 38 // A SignatureStream iterates over a Java descriptor (or parts of it).
 39 // The syntax is documented in the Java Virtual Machine Specification,
 40 // section 4.3.
 41 //
 42 // The syntax may be summarized as follows:
 43 //
 44 //     MethodType: '(' {FieldType}* ')' (FieldType | 'V')
 45 //     FieldType: PrimitiveType | ObjectType | ArrayType
 46 //     PrimitiveType: 'B' | 'C' | 'D' | 'F' | 'I' | 'J' | 'S' | 'Z'
 47 //     ObjectType: 'L' ClassName ';' | ArrayType
 48 //     ArrayType: '[' FieldType
 49 //     ClassName: {UnqualifiedName '/'}* UnqualifiedName
 50 //     UnqualifiedName: NameChar {NameChar}*
 51 //     NameChar: ANY_CHAR_EXCEPT('/' | '.' | ';' | '[')
 52 //
 53 // All of the concrete characters in the above grammar are given
 54 // standard manifest constant names of the form JVM_SIGNATURE_x.
 55 // Executable code uses these constant names in preference to raw
 56 // character constants.  Comments and assertion code sometimes use
 57 // the raw character constants for brevity.
 58 //
 59 // The primitive field types (like 'I') correspond 1-1 with type codes
 60 // (like T_INT) which form part of the specification of the 'newarray'
 61 // instruction (JVMS 6.5, section on newarray).  These type codes are
 62 // widely used in the HotSpot code.  They are joined by ad hoc codes
 63 // like T_OBJECT and T_ARRAY (defined in HotSpot but not in the JVMS)
 64 // so that each "basic type" of field descriptor (or void return type)
 65 // has a corresponding T_x code.  Thus, while T_x codes play a very
 66 // minor role in the JVMS, they play a major role in the HotSpot
 67 // sources.  There are fewer than 16 such "basic types", so they fit
 68 // nicely into bitfields.
 69 //
 70 // The syntax of ClassName overlaps slightly with the descriptor
 71 // syntaxes.  The strings "I" and "(I)V" are both class names
 72 // *and* descriptors.  If a class name contains any character other
 73 // than "BCDFIJSZ()V" it cannot be confused with a descriptor.
 74 // Class names inside of descriptors are always contained in an
 75 // "envelope" syntax which starts with 'L' and ends with ';'.
 76 //
 77 // As a confounding factor, array types report their type name strings
 78 // in descriptor format.  These name strings are easy to recognize,
 79 // since they begin with '['.  For this reason some API points on
 80 // HotSpot look for array descriptors as well as proper class names.
 81 //
 82 // For historical reasons some API points that accept class names and
 83 // array names also look for class names wrapped inside an envelope
 84 // (like "LFoo;") and unwrap them on the fly (to a name like "Foo").
 85 
 86 class Signature : AllStatic {
 87  private:
 88   static bool is_valid_array_signature(const Symbol* sig);
 89 
 90  public:
 91 
 92   // Returns the basic type of a field signature (or T_VOID for "V").
 93   // Assumes the signature is a valid field descriptor.
 94   // Do not apply this function to class names or method signatures.
 95   static BasicType basic_type(const Symbol* signature) {
 96     return basic_type(signature->char_at(0));
 97   }
 98 
 99   // Returns T_ILLEGAL for an illegal signature char.
100   static BasicType basic_type(int ch);
101 
102   // Assuming it is either a class name or signature,
103   // determine if it in fact is an array descriptor.
104   static bool is_array(const Symbol* signature) {
105     return (signature->utf8_length() > 1 &&
106             signature->char_at(0) == JVM_SIGNATURE_ARRAY &&
107             is_valid_array_signature(signature));
108   }
109 
110   // Assuming it is either a class name or signature,
111   // determine if it contains a class name plus ';'.
112   static bool has_envelope(const Symbol* signature) {
113     return ((signature->utf8_length() > 0) &&
114             signature->ends_with(JVM_SIGNATURE_ENDCLASS) &&
115             has_envelope(signature->char_at(0)));
116   }
117 
118   // Determine if this signature char introduces an
119   // envelope, which is a class name plus ';'.
120   static bool has_envelope(char signature_char) {
121     return (signature_char == JVM_SIGNATURE_CLASS);
122   }
123 
124   // Assuming has_envelope is true, return the symbol
125   // inside the envelope, by stripping 'L' and ';'.
126   // Caller is responsible for decrementing the newly created
127   // Symbol's refcount, use TempNewSymbol.
128   static Symbol* strip_envelope(const Symbol* signature);
129 
130   // Assuming it's either a field or method descriptor, determine
131   // whether it is in fact a method descriptor:
132   static bool is_method(const Symbol* signature) {
133     return signature->starts_with(JVM_SIGNATURE_FUNC);
134   }
135 
136   // Assuming it's a method signature, determine if it must
137   // return void.
138   static bool is_void_method(const Symbol* signature) {
139     assert(is_method(signature), "signature is not for a method");
140     return signature->ends_with(JVM_SIGNATURE_VOID);
141   }
142 };
143 
144 // A SignatureIterator uses a SignatureStream to produce BasicType
145 // results, discarding class names.  This means it can be accelerated
146 // using a fingerprint mechanism, in many cases, without loss of type
147 // information.  The FingerPrinter class computes and caches this
148 // reduced information for faster iteration.
149 
150 class SignatureIterator: public ResourceObj {
151  public:
152   typedef uint64_t fingerprint_t;
153 
154  protected:
155   Symbol*      _signature;             // the signature to iterate over
156   BasicType    _return_type;
157   fingerprint_t _fingerprint;
158 
159  public:
160   // Definitions used in generating and iterating the
161   // bit field form of the signature generated by the
162   // Fingerprinter.
163   enum {
164     fp_static_feature_size    = 1,
165     fp_is_static_bit          = 1,
166 
167     fp_result_feature_size    = 4,
168     fp_result_feature_mask    = right_n_bits(fp_result_feature_size),
169     fp_parameter_feature_size = 4,
170     fp_parameter_feature_mask = right_n_bits(fp_parameter_feature_size),
171 
172     fp_parameters_done        = 0,  // marker for end of parameters (must be zero)
173 
174     // Parameters take up full wordsize, minus the result and static bit fields.
175     // Since fp_parameters_done is zero, termination field arises from shifting
176     // in zero bits, and therefore occupies no extra space.
177     // The sentinel value is all-zero-bits, which is impossible for a true
178     // fingerprint, since at least the result field will be non-zero.
179     fp_max_size_of_parameters = ((BitsPerLong
180                                   - (fp_result_feature_size + fp_static_feature_size))
181                                  / fp_parameter_feature_size)
182   };
183 
184   static bool fp_is_valid_type(BasicType type, bool for_return_type = false);
185 
186   // Sentinel values are zero and not-zero (-1).
187   // No need to protect the sign bit, since every valid return type is non-zero
188   // (even T_VOID), and there are no valid parameter fields which are 0xF (T_VOID).
189   static fingerprint_t zero_fingerprint() { return (fingerprint_t)0; }
190   static fingerprint_t overflow_fingerprint() { return ~(fingerprint_t)0; }
191   static bool fp_is_valid(fingerprint_t fingerprint) {
192     return (fingerprint != zero_fingerprint()) && (fingerprint != overflow_fingerprint());
193   }
194 
195   // Constructors
196   SignatureIterator(Symbol* signature, fingerprint_t fingerprint = zero_fingerprint()) {
197     _signature   = signature;
198     _return_type = T_ILLEGAL;  // sentinel value for uninitialized
199     _fingerprint = zero_fingerprint();
200     if (fingerprint != _fingerprint) {
201       set_fingerprint(fingerprint);
202     }
203   }
204 
205   // If the fingerprint is present, we can use an accelerated loop.
206   void set_fingerprint(fingerprint_t fingerprint);
207 
208   // Returns the set fingerprint, or zero_fingerprint()
209   // if none has been set already.
210   fingerprint_t fingerprint() const { return _fingerprint; }
211 
212   // Iteration
213   // Hey look:  There are no virtual methods in this class.
214   // So how is it customized?  By calling do_parameters_on
215   // an object which answers to "do_type(BasicType)".
216   // By convention, this object is in the subclass
217   // itself, so the call is "do_parameters_on(this)".
218   // The effect of this is to inline the parsing loop
219   // everywhere "do_parameters_on" is called.
220   // If there is a valid fingerprint in the object,
221   // an improved loop is called which just unpacks the
222   // bitfields from the fingerprint.  Otherwise, the
223   // symbol is parsed.
224   template<typename T> inline void do_parameters_on(T* callback); // iterates over parameters only
225   BasicType return_type();  // computes the value on the fly if necessary
226 
227   static BasicType fp_return_type(fingerprint_t fingerprint) {
228     assert(fp_is_valid(fingerprint), "invalid fingerprint");
229     return (BasicType) ((fingerprint >> fp_static_feature_size) & fp_result_feature_mask);
230   }
231   static fingerprint_t fp_start_parameters(fingerprint_t fingerprint) {
232     assert(fp_is_valid(fingerprint), "invalid fingerprint");
233     return fingerprint >> (fp_static_feature_size + fp_result_feature_size);
234   }
235   static BasicType fp_next_parameter(fingerprint_t& mask) {
236     int result = (mask & fp_parameter_feature_mask);
237     mask >>= fp_parameter_feature_size;
238     return (BasicType) result;
239   }
240 };
241 
242 
243 // Specialized SignatureIterators: Used to compute signature specific values.
244 
245 class SignatureTypeNames : public SignatureIterator {
246  protected:
247   virtual void type_name(const char* name)   = 0;
248 
249   friend class SignatureIterator;  // so do_parameters_on can call do_type
250   void do_type(BasicType type) {
251     switch (type) {
252     case T_BOOLEAN: type_name("jboolean"); break;
253     case T_CHAR:    type_name("jchar"   ); break;
254     case T_FLOAT:   type_name("jfloat"  ); break;
255     case T_DOUBLE:  type_name("jdouble" ); break;
256     case T_BYTE:    type_name("jbyte"   ); break;
257     case T_SHORT:   type_name("jshort"  ); break;
258     case T_INT:     type_name("jint"    ); break;
259     case T_LONG:    type_name("jlong"   ); break;
260     case T_VOID:    type_name("void"    ); break;
261     case T_ARRAY:
262     case T_OBJECT:  type_name("jobject" ); break;
263     default: ShouldNotReachHere();
264     }
265   }
266 
267  public:
268   SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {}
269 };
270 
271 
272 // Specialized SignatureIterator: Used to compute the argument size.
273 
274 class ArgumentSizeComputer: public SignatureIterator {
275  private:
276   int _size;
277   friend class SignatureIterator;  // so do_parameters_on can call do_type
278   void do_type(BasicType type) { _size += parameter_type_word_count(type); }
279  public:
280   ArgumentSizeComputer(Symbol* signature);
281   int size() { return _size; }
282 };
283 
284 
285 class ArgumentCount: public SignatureIterator {
286  private:
287   int _size;
288   friend class SignatureIterator;  // so do_parameters_on can call do_type
289   void do_type(BasicType type) { _size++; }
290  public:
291   ArgumentCount(Symbol* signature);
292   int size() { return _size; }
293 };
294 
295 
296 class ReferenceArgumentCount: public SignatureIterator {
297  private:
298   int _refs;
299   friend class SignatureIterator;  // so do_parameters_on can call do_type
300   void do_type(BasicType type) { if (is_reference_type(type)) _refs++; }
301  public:
302   ReferenceArgumentCount(Symbol* signature);
303   int count() { return _refs; }
304 };
305 
306 
307 // Specialized SignatureIterator: Used to compute the result type.
308 
309 class ResultTypeFinder: public SignatureIterator {
310  public:
311   BasicType type() { return return_type(); }
312   ResultTypeFinder(Symbol* signature) : SignatureIterator(signature) { }
313 };
314 
315 
316 // Fingerprinter computes a unique ID for a given method. The ID
317 // is a bitvector characterizing the methods signature (incl. the receiver).
318 class Fingerprinter: public SignatureIterator {
319  private:
320   fingerprint_t _accumulator;
321   int _param_size;
322   int _stack_arg_slots;
323   int _shift_count;
324   const Method* _method;
325 
326   uint _int_args;
327   uint _fp_args;
328 
329   void initialize_accumulator() {
330     _accumulator = 0;
331     _shift_count = fp_result_feature_size + fp_static_feature_size;
332     _param_size = 0;
333     _stack_arg_slots = 0;
334   }
335 
336   // Out-of-line method does it all in constructor:
337   void compute_fingerprint_and_return_type(bool static_flag = false);
338 
339   void initialize_calling_convention(bool static_flag);
340   void do_type_calling_convention(BasicType type);
341 
342   friend class SignatureIterator;  // so do_parameters_on can call do_type
343   ATTRIBUTE_NO_UBSAN
344   void do_type(BasicType type) {
345     assert(fp_is_valid_type(type), "bad parameter type");
346     _accumulator |= ((fingerprint_t)type << _shift_count);
347     _shift_count += fp_parameter_feature_size;
348     _param_size += (is_double_word_type(type) ? 2 : 1);
349     do_type_calling_convention(type);
350   }
351 
352  public:
353   int size_of_parameters() const { return _param_size; }
354   int num_stack_arg_slots() const { return _stack_arg_slots; }
355 
356   // fingerprint() and return_type() are in super class
357 
358   Fingerprinter(const methodHandle& method)
359     : SignatureIterator(method->signature()),
360       _method(method()) {
361     compute_fingerprint_and_return_type();
362   }
363   Fingerprinter(Symbol* signature, bool is_static)
364     : SignatureIterator(signature),
365       _method(nullptr) {
366     compute_fingerprint_and_return_type(is_static);
367   }
368 };
369 
370 
371 // Specialized SignatureIterator: Used for native call purposes
372 
373 class NativeSignatureIterator: public SignatureIterator {
374  private:
375   methodHandle _method;
376 // We need separate JNI and Java offset values because in 64 bit mode,
377 // the argument offsets are not in sync with the Java stack.
378 // For example a long takes up 1 "C" stack entry but 2 Java stack entries.
379   int          _offset;                // The java stack offset
380   int          _prepended;             // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static)
381   int          _jni_offset;            // the current parameter offset, starting with 0
382 
383   friend class SignatureIterator;  // so do_parameters_on can call do_type
384   void do_type(BasicType type) {
385     switch (type) {
386     case T_BYTE:
387     case T_BOOLEAN:
388       pass_byte();  _jni_offset++; _offset++;
389       break;
390     case T_CHAR:
391     case T_SHORT:
392       pass_short();  _jni_offset++; _offset++;
393       break;
394     case T_INT:
395       pass_int();    _jni_offset++; _offset++;
396       break;
397     case T_FLOAT:
398       pass_float();  _jni_offset++; _offset++;
399       break;
400     case T_DOUBLE: {
401       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
402       pass_double(); _jni_offset += jni_offset; _offset += 2;
403       break;
404     }
405     case T_LONG: {
406       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
407       pass_long();   _jni_offset += jni_offset; _offset += 2;
408       break;
409     }
410     case T_ARRAY:
411     case T_OBJECT:
412       pass_object(); _jni_offset++; _offset++;
413       break;
414     default:
415       ShouldNotReachHere();
416     }
417   }
418 
419  public:
420   methodHandle method() const          { return _method; }
421   int          offset() const          { return _offset; }
422   int      jni_offset() const          { return _jni_offset + _prepended; }
423   bool      is_static() const          { return method()->is_static(); }
424   virtual void pass_int()              = 0;
425   virtual void pass_long()             = 0;
426   virtual void pass_object()           = 0;  // objects, arrays, inlines
427   virtual void pass_float()            = 0;
428   virtual void pass_byte()             { pass_int(); };
429   virtual void pass_short()            { pass_int(); };
430 #ifdef _LP64
431   virtual void pass_double()           = 0;
432 #else
433   virtual void pass_double()           { pass_long(); }  // may be same as long
434 #endif
435 
436   NativeSignatureIterator(const methodHandle& method) : SignatureIterator(method->signature()) {
437     _method = method;
438     _offset = 0;
439     _jni_offset = 0;
440 
441     const int JNIEnv_words = 1;
442     const int mirror_words = 1;
443     _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words;
444   }
445 
446   void iterate() { iterate(Fingerprinter(method()).fingerprint()); }
447 
448   // iterate() calls the 3 virtual methods according to the following invocation syntax:
449   //
450   // {pass_int | pass_long | pass_object}
451   //
452   // Arguments are handled from left to right (receiver first, if any).
453   // The offset() values refer to the Java stack offsets but are 0 based and increasing.
454   // The java_offset() values count down to 0, and refer to the Java TOS.
455   // The jni_offset() values increase from 1 or 2, and refer to C arguments.
456   // The method's return type is ignored.
457 
458   void iterate(fingerprint_t fingerprint) {
459     set_fingerprint(fingerprint);
460     if (!is_static()) {
461       // handle receiver (not handled by iterate because not in signature)
462       pass_object(); _jni_offset++; _offset++;
463     }
464     do_parameters_on(this);
465   }
466 };
467 
468 
469 // This is the core parsing logic for iterating over signatures.
470 // All of the previous classes use this for doing their work.
471 
472 class SignatureStream : public StackObj {
473  private:
474   const Symbol* _signature;
475   int          _begin;
476   int          _end;
477   int          _limit;
478   int          _array_prefix;  // count of '[' before the array element descr
479   BasicType    _type;
480   int          _state;
481   Symbol*      _previous_name;    // cache the previously looked up symbol to avoid lookups
482   GrowableArray<Symbol*>* _names; // symbols created while parsing that need to be dereferenced
483 
484   Symbol* find_symbol();
485 
486   enum { _s_field = 0, _s_method = 1, _s_method_return = 3 };
487   void set_done() {
488     _state |= -2;   // preserve s_method bit
489     assert(is_done(), "Unable to set state to done");
490   }
491   int scan_type(BasicType bt);
492 
493  public:
494   bool at_return_type() const                    { return _state == (int)_s_method_return; }
495   bool is_done() const                           { return _state < 0; }
496   void next();
497 
498   SignatureStream(const Symbol* signature, bool is_method = true);
499   ~SignatureStream();
500 
501   bool is_reference() const { return is_reference_type(_type); }
502   bool is_array() const     { return _type == T_ARRAY; }
503   BasicType type() const    { return _type; }
504 
505   const u1* raw_bytes() const  { return _signature->bytes() + _begin; }
506   int       raw_length() const { return _end - _begin; }
507   int raw_symbol_begin() const { return _begin + (has_envelope() ? 1 : 0); }
508   int raw_symbol_end() const   { return _end  -  (has_envelope() ? 1 : 0); }
509   char raw_char_at(int i) const {
510     assert(i < _limit, "index for raw_char_at is over the limit");
511     return _signature->char_at(i);
512   }
513 
514   // True if there is an embedded class name in this type,
515   // followed by ';'.
516   bool has_envelope() const {
517     if (!Signature::has_envelope(_signature->char_at(_begin)))
518       return false;
519     // this should always be true, but let's test it:
520     assert(_signature->char_at(_end-1) == JVM_SIGNATURE_ENDCLASS, "signature envelope has no semi-colon at end");
521     return true;
522   }
523 
524   // return the symbol for chars in symbol_begin()..symbol_end()
525   Symbol* as_symbol() {
526     return find_symbol();
527   }
528 
529   // in case you want only the return type:
530   void skip_to_return_type();
531 
532   // number of '[' in array prefix
533   int array_prefix_length() {
534     return _type == T_ARRAY ? _array_prefix : 0;
535   }
536 
537   // In case you want only the array base type,
538   // reset the stream after skipping some brackets '['.
539   // (The argument is clipped to array_prefix_length(),
540   // and if it ends up as zero this call is a nop.
541   // The default is value skips all brackets '['.)
542  private:
543   int skip_whole_array_prefix();
544  public:
545   int skip_array_prefix(int max_skip_length) {
546     if (_type != T_ARRAY) {
547       return 0;
548     }
549      if (_array_prefix > max_skip_length) {
550       // strip some but not all levels of T_ARRAY
551       _array_prefix -= max_skip_length;
552       _begin += max_skip_length;
553       return max_skip_length;
554     }
555     return skip_whole_array_prefix();
556   }
557   int skip_array_prefix() {
558     if (_type != T_ARRAY) {
559       return 0;
560     }
561     return skip_whole_array_prefix();
562   }
563 
564   // free-standing lookups (bring your own CL/PD pair)
565   enum FailureMode { ReturnNull, NCDFError, CachedOrNull };
566 
567   Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
568   InlineKlass* as_inline_klass(InstanceKlass* holder);
569   oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
570 };
571 
572 class SigEntryFilter;
573 typedef GrowableArrayFilterIterator<SigEntry, SigEntryFilter> ExtendedSignature;
574 
575 // Used for adapter generation. One SigEntry is used per element of
576 // the signature of the method. Inline type arguments are treated
577 // specially. See comment for InlineKlass::collect_fields().
578 class SigEntry {
579  public:
580   BasicType _bt;
581   int _offset;
582   Symbol* _symbol;
583 
584   SigEntry()
585     : _bt(T_ILLEGAL), _offset(-1), _symbol(NULL) {}
586 
587   SigEntry(BasicType bt, int offset, Symbol* symbol)
588     : _bt(bt), _offset(offset), _symbol(symbol) {}
589 
590   static int compare(SigEntry* e1, SigEntry* e2) {
591     if (e1->_offset != e2->_offset) {
592       return e1->_offset - e2->_offset;
593     }
594     assert((e1->_bt == T_LONG && (e2->_bt == T_LONG || e2->_bt == T_VOID)) ||
595            (e1->_bt == T_DOUBLE && (e2->_bt == T_DOUBLE || e2->_bt == T_VOID)) ||
596            e1->_bt == T_METADATA || e2->_bt == T_METADATA || e1->_bt == T_VOID || e2->_bt == T_VOID, "bad bt");
597     if (e1->_bt == e2->_bt) {
598       assert(e1->_bt == T_METADATA || e1->_bt == T_VOID, "only ones with duplicate offsets");
599       return 0;
600     }
601     if (e1->_bt == T_VOID ||
602         e2->_bt == T_METADATA) {
603       return 1;
604     }
605     if (e1->_bt == T_METADATA ||
606         e2->_bt == T_VOID) {
607       return -1;
608     }
609     ShouldNotReachHere();
610     return 0;
611   }
612   static void add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset = -1);
613   static bool skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i);
614   static int fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt);
615   static TempNewSymbol create_symbol(const GrowableArray<SigEntry>* sig);
616 };
617 
618 class SigEntryFilter {
619 public:
620   bool operator()(const SigEntry& entry) { return entry._bt != T_METADATA && entry._bt != T_VOID; }
621 };
622 
623 // Specialized SignatureStream: used for invoking SystemDictionary to either find
624 //                              or resolve the underlying type when iterating over a
625 //                              Java descriptor (or parts of it).
626 class ResolvingSignatureStream : public SignatureStream {
627   Klass*       _load_origin;
628   bool         _handles_cached;
629   Handle       _class_loader;       // cached when needed
630   Handle       _protection_domain;  // cached when needed
631 
632   void initialize_load_origin(Klass* load_origin) {
633     _load_origin = load_origin;
634     _handles_cached = (load_origin == nullptr);
635   }
636   void need_handles() {
637     if (!_handles_cached) {
638       cache_handles();
639       _handles_cached = true;
640     }
641   }
642   void cache_handles();
643 
644  public:
645   ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method = true);
646   ResolvingSignatureStream(Symbol* signature, Handle class_loader, Handle protection_domain, bool is_method = true);
647   ResolvingSignatureStream(const Method* method);
648 
649   Klass* as_klass(FailureMode failure_mode, TRAPS) {
650     need_handles();
651     return SignatureStream::as_klass(_class_loader, _protection_domain,
652                                      failure_mode, THREAD);
653   }
654   oop as_java_mirror(FailureMode failure_mode, TRAPS) {
655     if (is_reference()) {
656       need_handles();
657     }
658     return SignatureStream::as_java_mirror(_class_loader, _protection_domain,
659                                            failure_mode, THREAD);
660   }
661 };
662 
663 // Here is how all the SignatureIterator classes invoke the
664 // SignatureStream engine to do their parsing.
665 template<typename T> inline
666 void SignatureIterator::do_parameters_on(T* callback) {
667   fingerprint_t unaccumulator = _fingerprint;
668 
669   // Check for too many arguments, or missing fingerprint:
670   if (!fp_is_valid(unaccumulator)) {
671     SignatureStream ss(_signature);
672     for (; !ss.at_return_type(); ss.next()) {
673       callback->do_type(ss.type());
674     }
675     // while we are here, capture the return type
676     _return_type = ss.type();
677   } else {
678     // Optimized version of do_parameters when fingerprint is known
679     assert(_return_type != T_ILLEGAL, "return type already captured from fp");
680     unaccumulator = fp_start_parameters(unaccumulator);
681     for (BasicType type; (type = fp_next_parameter(unaccumulator)) != (BasicType)fp_parameters_done; ) {
682       assert(fp_is_valid_type(type), "garbled fingerprint");
683       callback->do_type(type);
684     }
685   }
686 }
687 
688 #ifdef ASSERT
689  class SignatureVerifier : public StackObj {
690   public:
691     static bool is_valid_method_signature(const Symbol* sig);
692     static bool is_valid_type_signature(const Symbol* sig);
693   private:
694     static ssize_t is_valid_type(const char*, ssize_t);
695 };
696 #endif
697 #endif // SHARE_RUNTIME_SIGNATURE_HPP