1 /*
  2  * Copyright (c) 1997, 2024, 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 #include "asm/assembler.hpp"
 26 #include "classfile/symbolTable.hpp"
 27 #include "classfile/systemDictionary.hpp"
 28 #include "classfile/vmSymbols.hpp"
 29 #include "memory/oopFactory.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "memory/universe.hpp"
 32 #include "oops/instanceKlass.hpp"
 33 #include "oops/klass.inline.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "oops/symbol.hpp"
 36 #include "oops/typeArrayKlass.hpp"
 37 #include "runtime/fieldDescriptor.inline.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 #include "runtime/safepointVerifiers.hpp"
 40 #include "runtime/sharedRuntime.hpp"
 41 #include "runtime/signature.hpp"
 42 #include "runtime/sharedRuntime.hpp"
 43 
 44 // Implementation of SignatureIterator
 45 
 46 // Signature syntax:
 47 //
 48 // Signature  = "(" {Parameter} ")" ReturnType.
 49 // Parameter  = FieldType.
 50 // ReturnType = FieldType | "V".
 51 // FieldType  = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
 52 // ClassName  = string.
 53 
 54 // The ClassName string can be any JVM-style UTF8 string except:
 55 //  - an empty string (the empty string is never a name of any kind)
 56 //  - a string which begins or ends with slash '/' (the package separator)
 57 //  - a string which contains adjacent slashes '//' (no empty package names)
 58 //  - a string which contains a semicolon ';' (the end-delimiter)
 59 //  - a string which contains a left bracket '[' (the array marker)
 60 //  - a string which contains a dot '.' (the external package separator)
 61 //
 62 // Other "meta-looking" characters, such as '(' and '<' and '+',
 63 // are perfectly legitimate within a class name, for the JVM.
 64 // Class names which contain double slashes ('a//b') and non-initial
 65 // brackets ('a[b]') are reserved for possible enrichment of the
 66 // type language.
 67 
 68 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
 69   if (!fp_is_valid(fingerprint)) {
 70     _fingerprint = fingerprint;
 71     _return_type = T_ILLEGAL;
 72   } else if (fingerprint != _fingerprint) {
 73     assert(_fingerprint == zero_fingerprint(), "consistent fingerprint values");
 74     _fingerprint = fingerprint;
 75     _return_type = fp_return_type(fingerprint);
 76   }
 77 }
 78 
 79 BasicType SignatureIterator::return_type() {
 80   if (_return_type == T_ILLEGAL) {
 81     SignatureStream ss(_signature);
 82     ss.skip_to_return_type();
 83     _return_type = ss.type();
 84     assert(_return_type != T_ILLEGAL, "illegal return type");
 85   }
 86   return _return_type;
 87 }
 88 
 89 bool SignatureIterator::fp_is_valid_type(BasicType type, bool for_return_type) {
 90   assert(type != (BasicType)fp_parameters_done, "fingerprint is incorrectly at done");
 91   assert(((int)type & ~fp_parameter_feature_mask) == 0, "fingerprint feature mask yielded non-zero value");
 92   return (is_java_primitive(type) ||
 93           is_reference_type(type) ||
 94           (for_return_type && type == T_VOID));
 95 }
 96 
 97 ArgumentSizeComputer::ArgumentSizeComputer(Symbol* signature)
 98   : SignatureIterator(signature)
 99 {
100   _size = 0;
101   do_parameters_on(this);  // non-virtual template execution
102 }
103 
104 ArgumentCount::ArgumentCount(Symbol* signature)
105   : SignatureIterator(signature)
106 {
107   _size = 0;
108   do_parameters_on(this);  // non-virtual template execution
109 }
110 
111 ReferenceArgumentCount::ReferenceArgumentCount(Symbol* signature)
112   : SignatureIterator(signature)
113 {
114   _refs = 0;
115   do_parameters_on(this);  // non-virtual template execution
116 }
117 
118 #if !defined(_LP64) || defined(ZERO) || defined(ASSERT)
119 static int compute_num_stack_arg_slots(Symbol* signature, int sizeargs, bool is_static) {
120   ResourceMark rm;
121   BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
122   VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
123 
124   int sig_index = 0;
125   if (!is_static) {
126     sig_bt[sig_index++] = T_OBJECT; // 'this'
127   }
128   for (SignatureStream ss(signature); !ss.at_return_type(); ss.next()) {
129     BasicType t = ss.type();
130     assert(type2size[t] == 1 || type2size[t] == 2, "size is 1 or 2");
131     sig_bt[sig_index++] = t;
132     if (type2size[t] == 2) {
133       sig_bt[sig_index++] = T_VOID;
134     }
135   }
136   assert(sig_index == sizeargs, "sig_index: %d sizeargs: %d", sig_index, sizeargs);
137 
138   return SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
139 }
140 #endif
141 
142 void Fingerprinter::compute_fingerprint_and_return_type(bool static_flag) {
143   // See if we fingerprinted this method already
144   if (_method != nullptr) {
145     assert(!static_flag, "must not be passed by caller");
146     static_flag = _method->is_static();
147     _fingerprint = _method->constMethod()->fingerprint();
148 
149     if (_fingerprint != zero_fingerprint()) {
150       _return_type = _method->result_type();
151       assert(is_java_type(_return_type), "return type must be a java type");
152       return;
153     }
154 
155     if (_method->size_of_parameters() > fp_max_size_of_parameters) {
156       _fingerprint = overflow_fingerprint();
157       _method->constMethod()->set_fingerprint(_fingerprint);
158       // as long as we are here compute the return type:
159       _return_type = ResultTypeFinder(_method->signature()).type();
160       assert(is_java_type(_return_type), "return type must be a java type");
161       return;
162     }
163   }
164 
165   // Note:  This will always take the slow path, since _fp==zero_fp.
166   initialize_accumulator();
167   initialize_calling_convention(static_flag);
168   do_parameters_on(this);
169   assert(fp_is_valid_type(_return_type, true), "bad result type");
170 
171   // Fill in the return type and static bits:
172   _accumulator |= _return_type << fp_static_feature_size;
173   if (static_flag) {
174     _accumulator |= fp_is_static_bit;
175   } else {
176     _param_size += 1;  // this is the convention for Method::compute_size_of_parameters
177   }
178 
179 #if defined(_LP64) && !defined(ZERO)
180 #ifdef ASSERT
181   int dbg_stack_arg_slots = compute_num_stack_arg_slots(_signature, _param_size, static_flag);
182   assert(_stack_arg_slots == dbg_stack_arg_slots, "fingerprinter: %d full: %d", _stack_arg_slots, dbg_stack_arg_slots);
183 #endif
184 #else
185   // Fallback: computed _stack_arg_slots is unreliable, compute directly.
186   _stack_arg_slots = compute_num_stack_arg_slots(_signature, _param_size, static_flag);
187 #endif
188 
189   // Detect overflow.  (We counted _param_size correctly.)
190   if (_method == nullptr && _param_size > fp_max_size_of_parameters) {
191     // We did a one-pass computation of argument size, return type,
192     // and fingerprint.
193     _fingerprint = overflow_fingerprint();
194     return;
195   }
196 
197   assert(_shift_count < BitsPerLong,
198          "shift count overflow %d (%d vs. %d): %s",
199          _shift_count, _param_size, fp_max_size_of_parameters,
200          _signature->as_C_string());
201   assert((_accumulator >> _shift_count) == fp_parameters_done, "must be zero");
202 
203   // This is the result, along with _return_type:
204   _fingerprint = _accumulator;
205 
206   // Cache the result on the method itself:
207   if (_method != nullptr) {
208     _method->constMethod()->set_fingerprint(_fingerprint);
209   }
210 }
211 
212 void Fingerprinter::initialize_calling_convention(bool static_flag) {
213   _int_args = 0;
214   _fp_args = 0;
215 
216   if (!static_flag) { // `this` takes up an int register
217     _int_args++;
218   }
219 }
220 
221 void Fingerprinter::do_type_calling_convention(BasicType type) {
222   // We compute the number of slots for stack-passed arguments in compiled calls.
223   // TODO: SharedRuntime::java_calling_convention is the shared code that knows all details
224   // about the platform-specific calling conventions. This method tries to compute the stack
225   // args number... poorly, at least for 32-bit ports and for zero. Current code has the fallback
226   // that recomputes the stack args number from SharedRuntime::java_calling_convention.
227 #if defined(_LP64) && !defined(ZERO)
228   switch (type) {
229   case T_VOID:
230     break;
231   case T_BOOLEAN:
232   case T_CHAR:
233   case T_BYTE:
234   case T_SHORT:
235   case T_INT:
236     if (_int_args < Argument::n_int_register_parameters_j) {
237       _int_args++;
238     } else {
239 #if defined(PPC64) || defined(S390)
240       _stack_arg_slots += 1;
241 #else
242       _stack_arg_slots = align_up(_stack_arg_slots, 2);
243       _stack_arg_slots += 1;
244 #endif // defined(PPC64) || defined(S390)
245     }
246     break;
247   case T_LONG:
248   case T_OBJECT:
249   case T_ARRAY:
250   case T_ADDRESS:
251     if (_int_args < Argument::n_int_register_parameters_j) {
252       _int_args++;
253     } else {
254       _stack_arg_slots = align_up(_stack_arg_slots, 2);
255       _stack_arg_slots += 2;
256     }
257     break;
258   case T_FLOAT:
259     if (_fp_args < Argument::n_float_register_parameters_j) {
260       _fp_args++;
261     } else {
262 #if defined(PPC64) || defined(S390)
263       _stack_arg_slots += 1;
264 #else
265       _stack_arg_slots = align_up(_stack_arg_slots, 2);
266       _stack_arg_slots += 1;
267 #endif // defined(PPC64) || defined(S390)
268     }
269     break;
270   case T_DOUBLE:
271     if (_fp_args < Argument::n_float_register_parameters_j) {
272       _fp_args++;
273     } else {
274       _stack_arg_slots = align_up(_stack_arg_slots, 2);
275       _stack_arg_slots += 2;
276     }
277     break;
278   default:
279     ShouldNotReachHere();
280     break;
281   }
282 #endif
283 }
284 
285 // Implementation of SignatureStream
286 
287 static inline BasicType decode_signature_char(int ch) {
288   switch (ch) {
289 #define EACH_SIG(ch, bt, ignore) \
290     case ch: return bt;
291     SIGNATURE_TYPES_DO(EACH_SIG, ignore)
292 #undef EACH_SIG
293   }
294   return (BasicType)0;
295 }
296 
297 SignatureStream::SignatureStream(const Symbol* signature,
298                                  bool is_method) {
299   assert(!is_method || signature->starts_with(JVM_SIGNATURE_FUNC),
300          "method signature required");
301   _signature = signature;
302   _limit = signature->utf8_length();
303   int oz = (is_method ? _s_method : _s_field);
304   _state = oz;
305   _begin = _end = oz; // skip first '(' in method signatures
306   _array_prefix = 0;  // just for definiteness
307 
308   // assigning java/lang/Object to _previous_name means we can
309   // avoid a number of null checks in the parser
310   _previous_name = vmSymbols::java_lang_Object();
311   _names = nullptr;
312   next();
313 }
314 
315 SignatureStream::~SignatureStream() {
316   if (_previous_name == vmSymbols::java_lang_Object()) {
317     // no names were created
318     assert(_names == nullptr, "_names unexpectedly created");
319     return;
320   }
321 
322   // decrement refcount for names created during signature parsing
323   _previous_name->decrement_refcount();
324   if (_names != nullptr) {
325     for (int i = 0; i < _names->length(); i++) {
326       _names->at(i)->decrement_refcount();
327     }
328   }
329 }
330 
331 inline int SignatureStream::scan_type(BasicType type) {
332   const u1* base = _signature->bytes();
333   int end = _end;
334   int limit = _limit;
335   const u1* tem;
336   switch (type) {
337   case T_OBJECT:
338     tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
339     return (tem == nullptr ? limit : pointer_delta_as_int(tem + 1, base));
340 
341   case T_ARRAY:
342     while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; }
343     // If we discovered only the string of '[', this means something is wrong.
344     if (end >= limit) {
345       assert(false, "Invalid type detected");
346       return limit;
347     }
348     _array_prefix = end - _end;  // number of '[' chars just skipped
349     if (Signature::has_envelope(base[end])) {
350       tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
351       return (tem == nullptr ? limit : pointer_delta_as_int(tem + 1, base));
352     }
353     // Skipping over a single character for a primitive type.
354     assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected");
355     return end + 1;
356 
357   default:
358     // Skipping over a single character for a primitive type (or void).
359     assert(!is_reference_type(type), "only primitives or void expected");
360     return end + 1;
361   }
362 }
363 
364 void SignatureStream::next() {
365   const Symbol* sig = _signature;
366   int len = _limit;
367   if (_end >= len) { set_done(); return; }
368   _begin = _end;
369   int ch = sig->char_at(_begin);
370   if (ch == JVM_SIGNATURE_ENDFUNC) {
371     assert(_state == _s_method, "must be in method");
372     _state = _s_method_return;
373     _begin = ++_end;
374     if (_end >= len) { set_done(); return; }
375     ch = sig->char_at(_begin);
376   }
377   BasicType bt = decode_signature_char(ch);
378   assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch);
379   _type = bt;
380   _end = scan_type(bt);
381 }
382 
383 int SignatureStream::skip_whole_array_prefix() {
384   assert(_type == T_ARRAY, "must be");
385 
386   // we are stripping all levels of T_ARRAY,
387   // so we must decode the next character
388   int whole_array_prefix = _array_prefix;
389   int new_begin = _begin + whole_array_prefix;
390   _begin = new_begin;
391   int ch = _signature->char_at(new_begin);
392   BasicType bt = decode_signature_char(ch);
393   assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch);
394   _type = bt;
395   assert(bt != T_VOID && bt != T_ARRAY, "bad signature type");
396   // Don't bother to re-scan, since it won't change the value of _end.
397   return whole_array_prefix;
398 }
399 
400 bool Signature::is_valid_array_signature(const Symbol* sig) {
401   assert(sig->utf8_length() > 1, "this should already have been checked");
402   assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
403   // The first character is already checked
404   int i = 1;
405   int len = sig->utf8_length();
406   // First skip all '['s
407   while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
408 
409   // Check type
410   switch(sig->char_at(i)) {
411   case JVM_SIGNATURE_BYTE:
412   case JVM_SIGNATURE_CHAR:
413   case JVM_SIGNATURE_DOUBLE:
414   case JVM_SIGNATURE_FLOAT:
415   case JVM_SIGNATURE_INT:
416   case JVM_SIGNATURE_LONG:
417   case JVM_SIGNATURE_SHORT:
418   case JVM_SIGNATURE_BOOLEAN:
419     // If it is an array, the type is the last character
420     return (i + 1 == len);
421   case JVM_SIGNATURE_CLASS:
422     // If it is an object, the last character must be a ';'
423     return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
424   }
425   return false;
426 }
427 
428 BasicType Signature::basic_type(int ch) {
429   BasicType btcode = decode_signature_char(ch);
430   if (btcode == 0)  return T_ILLEGAL;
431   return btcode;
432 }
433 
434 Symbol* Signature::strip_envelope(const Symbol* signature) {
435   assert(has_envelope(signature), "precondition");
436   return SymbolTable::new_symbol((char*) signature->bytes() + 1,
437                                  signature->utf8_length() - 2);
438 }
439 
440 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
441 static const char jl_str[] = "java/lang/";
442 
443 #ifdef ASSERT
444 static bool signature_symbols_sane() {
445   static bool done;
446   if (done)  return true;
447   done = true;
448   // test some tense code that looks for common symbol names:
449   assert(vmSymbols::java_lang_Object()->utf8_length() == jl_object_len &&
450          vmSymbols::java_lang_Object()->starts_with(jl_str, jl_len) &&
451          vmSymbols::java_lang_Object()->ends_with("Object", object_len) &&
452          vmSymbols::java_lang_Object()->is_permanent() &&
453          vmSymbols::java_lang_String()->utf8_length() == jl_object_len &&
454          vmSymbols::java_lang_String()->starts_with(jl_str, jl_len) &&
455          vmSymbols::java_lang_String()->ends_with("String", object_len) &&
456          vmSymbols::java_lang_String()->is_permanent(),
457          "sanity");
458   return true;
459 }
460 #endif //ASSERT
461 
462 // returns a symbol; the caller is responsible for decrementing it
463 Symbol* SignatureStream::find_symbol() {
464   // Create a symbol from for string _begin _end
465   int begin = raw_symbol_begin();
466   int end   = raw_symbol_end();
467 
468   const char* symbol_chars = (const char*)_signature->base() + begin;
469   int len = end - begin;
470 
471   // Quick check for common symbols in signatures
472   assert(signature_symbols_sane(), "incorrect signature sanity check");
473   if (len == jl_object_len &&
474       memcmp(symbol_chars, jl_str, jl_len) == 0) {
475     if (memcmp("String", symbol_chars + jl_len, object_len) == 0) {
476       return vmSymbols::java_lang_String();
477     } else if (memcmp("Object", symbol_chars + jl_len, object_len) == 0) {
478       return vmSymbols::java_lang_Object();
479     }
480   }
481 
482   Symbol* name = _previous_name;
483   if (name->equals(symbol_chars, len)) {
484     return name;
485   }
486 
487   // Save names for cleaning up reference count at the end of
488   // SignatureStream scope.
489   name = SymbolTable::new_symbol(symbol_chars, len);
490 
491   // Only allocate the GrowableArray for the _names buffer if more than
492   // one name is being processed in the signature.
493   if (!_previous_name->is_permanent()) {
494     if (_names == nullptr) {
495       _names = new GrowableArray<Symbol*>(10);
496     }
497     _names->push(_previous_name);
498   }
499   _previous_name = name;
500   return name;
501 }
502 
503 Klass* SignatureStream::as_klass(Handle class_loader, FailureMode failure_mode, TRAPS) {
504   if (!is_reference()) {
505     return nullptr;
506   }
507   Symbol* name = as_symbol();
508   Klass* k = nullptr;
509   if (failure_mode == ReturnNull) {
510     // Note:  SD::resolve_or_null returns null for most failure modes,
511     // but not all.  Circularity errors, invalid PDs, etc., throw.
512     k = SystemDictionary::resolve_or_null(name, class_loader, CHECK_NULL);
513   } else if (failure_mode == CachedOrNull) {
514     NoSafepointVerifier nsv;  // no loading, now, we mean it!
515     assert(!HAS_PENDING_EXCEPTION, "");
516     k = SystemDictionary::find_instance_klass(THREAD, name, class_loader);
517     // SD::find does not trigger loading, so there should be no throws
518     // Still, bad things can happen, so we CHECK_NULL and ask callers
519     // to do likewise.
520     return k;
521   } else {
522     // The only remaining failure mode is NCDFError.
523     // The test here allows for an additional mode CNFException
524     // if callers need to request the reflective error instead.
525     bool throw_error = (failure_mode == NCDFError);
526     k = SystemDictionary::resolve_or_fail(name, class_loader, throw_error, CHECK_NULL);
527   }
528 
529   return k;
530 }
531 
532 oop SignatureStream::as_java_mirror(Handle class_loader, FailureMode failure_mode, TRAPS) {
533   if (!is_reference()) {
534     return Universe::java_mirror(type());
535   }
536   Klass* klass = as_klass(class_loader, failure_mode, CHECK_NULL);
537   if (klass == nullptr) {
538     return nullptr;
539   }
540   return klass->java_mirror();
541 }
542 
543 void SignatureStream::skip_to_return_type() {
544   while (!at_return_type()) {
545     next();
546   }
547 }
548 
549 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
550                                                    Handle class_loader,
551                                                    bool is_method)
552   : SignatureStream(signature, is_method), _class_loader(class_loader)
553 {
554   initialize_load_origin(nullptr);
555 }
556 
557 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
558   : SignatureStream(signature, is_method)
559 {
560   assert(load_origin != nullptr, "");
561   initialize_load_origin(load_origin);
562 }
563 
564 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
565   : SignatureStream(method->signature(), true)
566 {
567   initialize_load_origin(method->method_holder());
568 }
569 
570 void ResolvingSignatureStream::cache_handles() {
571   assert(_load_origin != nullptr, "");
572   JavaThread* current = JavaThread::current();
573   _class_loader = Handle(current, _load_origin->class_loader());
574 }
575 
576 #ifdef ASSERT
577 
578 extern bool signature_constants_sane(); // called from basic_types_init()
579 
580 bool signature_constants_sane() {
581   // for the lookup table, test every 8-bit code point, and then some:
582   for (int i = -256; i <= 256; i++) {
583     int btcode = 0;
584     switch (i) {
585 #define EACH_SIG(ch, bt, ignore) \
586     case ch: { btcode = bt; break; }
587     SIGNATURE_TYPES_DO(EACH_SIG, ignore)
588 #undef EACH_SIG
589     }
590     int btc = decode_signature_char(i);
591     assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
592   }
593   return true;
594 }
595 
596 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
597   const char* method_sig = (const char*)sig->bytes();
598   ssize_t len = sig->utf8_length();
599   ssize_t index = 0;
600   if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
601     ++index;
602     while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
603       ssize_t res = is_valid_type(&method_sig[index], len - index);
604       if (res == -1) {
605         return false;
606       } else {
607         index += res;
608       }
609     }
610     if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
611       // check the return type
612       ++index;
613       return (is_valid_type(&method_sig[index], len - index) == (len - index));
614     }
615   }
616   return false;
617 }
618 
619 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
620   const char* type_sig = (const char*)sig->bytes();
621   ssize_t len = sig->utf8_length();
622   return (type_sig != nullptr && len >= 1 &&
623           (is_valid_type(type_sig, len) == len));
624 }
625 
626 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
627 // Returns -1 if it is not, or the index of the next character that is not part
628 // of the type.  The type encoding may end before 'limit' and that's ok.
629 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
630   ssize_t index = 0;
631 
632   // Iterate over any number of array dimensions
633   while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
634   if (index >= limit) {
635     return -1;
636   }
637   switch (type[index]) {
638     case JVM_SIGNATURE_BYTE:
639     case JVM_SIGNATURE_CHAR:
640     case JVM_SIGNATURE_FLOAT:
641     case JVM_SIGNATURE_DOUBLE:
642     case JVM_SIGNATURE_INT:
643     case JVM_SIGNATURE_LONG:
644     case JVM_SIGNATURE_SHORT:
645     case JVM_SIGNATURE_BOOLEAN:
646     case JVM_SIGNATURE_VOID:
647       return index + 1;
648     case JVM_SIGNATURE_CLASS:
649       for (index = index + 1; index < limit; ++index) {
650         char c = type[index];
651         switch (c) {
652           case JVM_SIGNATURE_ENDCLASS:
653             return index + 1;
654           case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
655             return -1;
656           default: ; // fall through
657         }
658       }
659       // fall through
660     default: ; // fall through
661   }
662   return -1;
663 }
664 
665 #endif // ASSERT