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