< prev index next >

src/hotspot/share/runtime/signature.cpp

Print this page

 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 "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/signature.hpp"
 41 
 42 // Implementation of SignatureIterator
 43 
 44 // Signature syntax:
 45 //
 46 // Signature  = "(" {Parameter} ")" ReturnType.
 47 // Parameter  = FieldType.
 48 // ReturnType = FieldType | "V".
 49 // FieldType  = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
 50 // ClassName  = string.
 51 
 52 // The ClassName string can be any JVM-style UTF8 string except:
 53 //  - an empty string (the empty string is never a name of any kind)
 54 //  - a string which begins or ends with slash '/' (the package separator)
 55 //  - a string which contains adjacent slashes '//' (no empty package names)
 56 //  - a string which contains a semicolon ';' (the end-delimiter)
 57 //  - a string which contains a left bracket '[' (the array marker)
 58 //  - a string which contains a dot '.' (the external package separator)
 59 //
 60 // Other "meta-looking" characters, such as '(' and '<' and '+',
 61 // are perfectly legitimate within a class name, for the JVM.
 62 // Class names which contain double slashes ('a//b') and non-initial
 63 // brackets ('a[b]') are reserved for possible enrichment of the
 64 // type language.
 65 
 66 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
 67   if (!fp_is_valid(fingerprint)) {
 68     _fingerprint = fingerprint;
 69     _return_type = T_ILLEGAL;

208     assert(_names == NULL, "_names unexpectedly created");
209     return;
210   }
211 
212   // decrement refcount for names created during signature parsing
213   _previous_name->decrement_refcount();
214   if (_names != NULL) {
215     for (int i = 0; i < _names->length(); i++) {
216       _names->at(i)->decrement_refcount();
217     }
218   }
219 }
220 
221 inline int SignatureStream::scan_type(BasicType type) {
222   const u1* base = _signature->bytes();
223   int end = _end;
224   int limit = _limit;
225   const u1* tem;
226   switch (type) {
227   case T_OBJECT:

228     tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
229     return (tem == NULL ? limit : tem + 1 - base);
230 
231   case T_ARRAY:
232     while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; }
233     _array_prefix = end - _end;  // number of '[' chars just skipped
234     if (Signature::has_envelope(base[end])) {
235       tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
236       return (tem == NULL ? limit : tem + 1 - base);
237     }
238     // Skipping over a single character for a primitive type.
239     assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected");
240     return end + 1;
241 
242   default:
243     // Skipping over a single character for a primitive type (or void).
244     assert(!is_reference_type(type), "only primitives or void expected");
245     return end + 1;
246   }
247 }

287   assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
288   // The first character is already checked
289   int i = 1;
290   int len = sig->utf8_length();
291   // First skip all '['s
292   while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
293 
294   // Check type
295   switch(sig->char_at(i)) {
296   case JVM_SIGNATURE_BYTE:
297   case JVM_SIGNATURE_CHAR:
298   case JVM_SIGNATURE_DOUBLE:
299   case JVM_SIGNATURE_FLOAT:
300   case JVM_SIGNATURE_INT:
301   case JVM_SIGNATURE_LONG:
302   case JVM_SIGNATURE_SHORT:
303   case JVM_SIGNATURE_BOOLEAN:
304     // If it is an array, the type is the last character
305     return (i + 1 == len);
306   case JVM_SIGNATURE_CLASS:

307     // If it is an object, the last character must be a ';'
308     return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
309   }
310   return false;
311 }
312 
313 BasicType Signature::basic_type(int ch) {
314   BasicType btcode = decode_signature_char(ch);
315   if (btcode == 0)  return T_ILLEGAL;
316   return btcode;
317 }
318 
319 Symbol* Signature::strip_envelope(const Symbol* signature) {
320   assert(has_envelope(signature), "precondition");
321   return SymbolTable::new_symbol((char*) signature->bytes() + 1,
322                                  signature->utf8_length() - 2);
323 }
324 
325 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
326 static const char jl_str[] = "java/lang/";

368   if (name->equals(symbol_chars, len)) {
369     return name;
370   }
371 
372   // Save names for cleaning up reference count at the end of
373   // SignatureStream scope.
374   name = SymbolTable::new_symbol(symbol_chars, len);
375 
376   // Only allocate the GrowableArray for the _names buffer if more than
377   // one name is being processed in the signature.
378   if (!_previous_name->is_permanent()) {
379     if (_names == NULL) {
380       _names = new GrowableArray<Symbol*>(10);
381     }
382     _names->push(_previous_name);
383   }
384   _previous_name = name;
385   return name;
386 }
387 









388 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
389                                  FailureMode failure_mode, TRAPS) {
390   if (!is_reference()) {
391     return NULL;
392   }
393   Symbol* name = as_symbol();
394   Klass* k = NULL;
395   if (failure_mode == ReturnNull) {
396     // Note:  SD::resolve_or_null returns NULL for most failure modes,
397     // but not all.  Circularity errors, invalid PDs, etc., throw.
398     k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
399   } else if (failure_mode == CachedOrNull) {
400     NoSafepointVerifier nsv;  // no loading, now, we mean it!
401     assert(!HAS_PENDING_EXCEPTION, "");
402     k = SystemDictionary::find_instance_klass(name, class_loader, protection_domain);
403     // SD::find does not trigger loading, so there should be no throws
404     // Still, bad things can happen, so we CHECK_NULL and ask callers
405     // to do likewise.
406     return k;
407   } else {
408     // The only remaining failure mode is NCDFError.
409     // The test here allows for an additional mode CNFException
410     // if callers need to request the reflective error instead.
411     bool throw_error = (failure_mode == NCDFError);
412     k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
413   }
414 
415   return k;
416 }
417 
418 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
419                                     FailureMode failure_mode, TRAPS) {
420   if (!is_reference()) {
421     return Universe::java_mirror(type());
422   }
423   Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
424   if (klass == NULL) {
425     return NULL;
426   }
427   return klass->java_mirror();

428 }
429 
430 void SignatureStream::skip_to_return_type() {
431   while (!at_return_type()) {
432     next();
433   }
434 }
435 
436 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
437                                                    Handle class_loader,
438                                                    Handle protection_domain,
439                                                    bool is_method)
440   : SignatureStream(signature, is_method),
441     _class_loader(class_loader), _protection_domain(protection_domain)
442 {
443   initialize_load_origin(NULL);
444 }
445 
446 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
447   : SignatureStream(signature, is_method)

464 
465 void ResolvingSignatureStream::cache_handles() {
466   assert(_load_origin != NULL, "");
467   JavaThread* current = JavaThread::current();
468   _class_loader = Handle(current, _load_origin->class_loader());
469   _protection_domain = Handle(current, _load_origin->protection_domain());
470 }
471 
472 Klass* ResolvingSignatureStream::as_klass_if_loaded(TRAPS) {
473   Klass* klass = as_klass(CachedOrNull, THREAD);
474   // SD::find does not trigger loading, so there should be no throws
475   // Still, bad things can happen, so we CHECK_NULL and ask callers
476   // to do likewise.
477   if (HAS_PENDING_EXCEPTION) {
478     CLEAR_PENDING_EXCEPTION;
479   }
480   return klass;
481 }
482 
483 #ifdef ASSERT
484 
485 extern bool signature_constants_sane(); // called from basic_types_init()
486 
487 bool signature_constants_sane() {
488   // for the lookup table, test every 8-bit code point, and then some:
489   for (int i = -256; i <= 256; i++) {
490     int btcode = 0;
491     switch (i) {
492 #define EACH_SIG(ch, bt, ignore) \
493     case ch: { btcode = bt; break; }
494     SIGNATURE_TYPES_DO(EACH_SIG, ignore)
495 #undef EACH_SIG
496     }
497     int btc = decode_signature_char(i);
498     assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
499   }
500   return true;
501 }
502 
503 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
504   const char* method_sig = (const char*)sig->bytes();
505   ssize_t len = sig->utf8_length();
506   ssize_t index = 0;
507   if (method_sig != NULL && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
508     ++index;
509     while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
510       ssize_t res = is_valid_type(&method_sig[index], len - index);
511       if (res == -1) {
512         return false;
513       } else {
514         index += res;
515       }
516     }
517     if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
518       // check the return type
519       ++index;
520       return (is_valid_type(&method_sig[index], len - index) == (len - index));
521     }
522   }
523   return false;
524 }
525 
526 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
527   const char* type_sig = (const char*)sig->bytes();
528   ssize_t len = sig->utf8_length();
529   return (type_sig != NULL && len >= 1 &&
530           (is_valid_type(type_sig, len) == len));
531 }
532 
533 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
534 // Returns -1 if it is not, or the index of the next character that is not part
535 // of the type.  The type encoding may end before 'limit' and that's ok.
536 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
537   ssize_t index = 0;
538 
539   // Iterate over any number of array dimensions
540   while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
541   if (index >= limit) {
542     return -1;
543   }
544   switch (type[index]) {
545     case JVM_SIGNATURE_BYTE:
546     case JVM_SIGNATURE_CHAR:
547     case JVM_SIGNATURE_FLOAT:
548     case JVM_SIGNATURE_DOUBLE:
549     case JVM_SIGNATURE_INT:
550     case JVM_SIGNATURE_LONG:
551     case JVM_SIGNATURE_SHORT:
552     case JVM_SIGNATURE_BOOLEAN:
553     case JVM_SIGNATURE_VOID:
554       return index + 1;

555     case JVM_SIGNATURE_CLASS:
556       for (index = index + 1; index < limit; ++index) {
557         char c = type[index];
558         switch (c) {
559           case JVM_SIGNATURE_ENDCLASS:
560             return index + 1;
561           case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
562             return -1;
563           default: ; // fall through
564         }
565       }
566       // fall through
567     default: ; // fall through
568   }
569   return -1;
570 }
571 
572 #endif // ASSERT






















































 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 "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 "oops/inlineKlass.inline.hpp"
 38 #include "runtime/fieldDescriptor.inline.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/safepointVerifiers.hpp"
 41 #include "runtime/signature.hpp"
 42 
 43 // Implementation of SignatureIterator
 44 
 45 // Signature syntax:
 46 //
 47 // Signature  = "(" {Parameter} ")" ReturnType.
 48 // Parameter  = FieldType.
 49 // ReturnType = FieldType | "V".
 50 // FieldType  = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "Q" ValueClassName ";" | "[" FieldType.
 51 // ClassName  = string.
 52 
 53 // The ClassName string can be any JVM-style UTF8 string except:
 54 //  - an empty string (the empty string is never a name of any kind)
 55 //  - a string which begins or ends with slash '/' (the package separator)
 56 //  - a string which contains adjacent slashes '//' (no empty package names)
 57 //  - a string which contains a semicolon ';' (the end-delimiter)
 58 //  - a string which contains a left bracket '[' (the array marker)
 59 //  - a string which contains a dot '.' (the external package separator)
 60 //
 61 // Other "meta-looking" characters, such as '(' and '<' and '+',
 62 // are perfectly legitimate within a class name, for the JVM.
 63 // Class names which contain double slashes ('a//b') and non-initial
 64 // brackets ('a[b]') are reserved for possible enrichment of the
 65 // type language.
 66 
 67 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
 68   if (!fp_is_valid(fingerprint)) {
 69     _fingerprint = fingerprint;
 70     _return_type = T_ILLEGAL;

209     assert(_names == NULL, "_names unexpectedly created");
210     return;
211   }
212 
213   // decrement refcount for names created during signature parsing
214   _previous_name->decrement_refcount();
215   if (_names != NULL) {
216     for (int i = 0; i < _names->length(); i++) {
217       _names->at(i)->decrement_refcount();
218     }
219   }
220 }
221 
222 inline int SignatureStream::scan_type(BasicType type) {
223   const u1* base = _signature->bytes();
224   int end = _end;
225   int limit = _limit;
226   const u1* tem;
227   switch (type) {
228   case T_OBJECT:
229   case T_INLINE_TYPE:
230     tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
231     return (tem == NULL ? limit : tem + 1 - base);
232 
233   case T_ARRAY:
234     while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; }
235     _array_prefix = end - _end;  // number of '[' chars just skipped
236     if (Signature::has_envelope(base[end])) {
237       tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
238       return (tem == NULL ? limit : tem + 1 - base);
239     }
240     // Skipping over a single character for a primitive type.
241     assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected");
242     return end + 1;
243 
244   default:
245     // Skipping over a single character for a primitive type (or void).
246     assert(!is_reference_type(type), "only primitives or void expected");
247     return end + 1;
248   }
249 }

289   assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
290   // The first character is already checked
291   int i = 1;
292   int len = sig->utf8_length();
293   // First skip all '['s
294   while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
295 
296   // Check type
297   switch(sig->char_at(i)) {
298   case JVM_SIGNATURE_BYTE:
299   case JVM_SIGNATURE_CHAR:
300   case JVM_SIGNATURE_DOUBLE:
301   case JVM_SIGNATURE_FLOAT:
302   case JVM_SIGNATURE_INT:
303   case JVM_SIGNATURE_LONG:
304   case JVM_SIGNATURE_SHORT:
305   case JVM_SIGNATURE_BOOLEAN:
306     // If it is an array, the type is the last character
307     return (i + 1 == len);
308   case JVM_SIGNATURE_CLASS:
309   case JVM_SIGNATURE_INLINE_TYPE:
310     // If it is an object, the last character must be a ';'
311     return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
312   }
313   return false;
314 }
315 
316 BasicType Signature::basic_type(int ch) {
317   BasicType btcode = decode_signature_char(ch);
318   if (btcode == 0)  return T_ILLEGAL;
319   return btcode;
320 }
321 
322 Symbol* Signature::strip_envelope(const Symbol* signature) {
323   assert(has_envelope(signature), "precondition");
324   return SymbolTable::new_symbol((char*) signature->bytes() + 1,
325                                  signature->utf8_length() - 2);
326 }
327 
328 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
329 static const char jl_str[] = "java/lang/";

371   if (name->equals(symbol_chars, len)) {
372     return name;
373   }
374 
375   // Save names for cleaning up reference count at the end of
376   // SignatureStream scope.
377   name = SymbolTable::new_symbol(symbol_chars, len);
378 
379   // Only allocate the GrowableArray for the _names buffer if more than
380   // one name is being processed in the signature.
381   if (!_previous_name->is_permanent()) {
382     if (_names == NULL) {
383       _names = new GrowableArray<Symbol*>(10);
384     }
385     _names->push(_previous_name);
386   }
387   _previous_name = name;
388   return name;
389 }
390 
391 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) {
392   JavaThread* THREAD = JavaThread::current();
393   Handle class_loader(THREAD, holder->class_loader());
394   Handle protection_domain(THREAD, holder->protection_domain());
395   Klass* k = as_klass(class_loader, protection_domain, SignatureStream::ReturnNull, THREAD);
396   assert(k != NULL && !HAS_PENDING_EXCEPTION, "unresolved inline klass");
397   return InlineKlass::cast(k);
398 }
399 
400 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
401                                  FailureMode failure_mode, TRAPS) {
402   if (!is_reference()) {
403     return NULL;
404   }
405   Symbol* name = as_symbol();
406   Klass* k = NULL;
407   if (failure_mode == ReturnNull) {
408     // Note:  SD::resolve_or_null returns NULL for most failure modes,
409     // but not all.  Circularity errors, invalid PDs, etc., throw.
410     k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
411   } else if (failure_mode == CachedOrNull) {
412     NoSafepointVerifier nsv;  // no loading, now, we mean it!
413     assert(!HAS_PENDING_EXCEPTION, "");
414     k = SystemDictionary::find_instance_klass(name, class_loader, protection_domain);
415     // SD::find does not trigger loading, so there should be no throws
416     // Still, bad things can happen, so we CHECK_NULL and ask callers
417     // to do likewise.
418     return k;
419   } else {
420     // The only remaining failure mode is NCDFError.
421     // The test here allows for an additional mode CNFException
422     // if callers need to request the reflective error instead.
423     bool throw_error = (failure_mode == NCDFError);
424     k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
425   }
426 
427   return k;
428 }
429 
430 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
431                                     FailureMode failure_mode, TRAPS) {
432   if (!is_reference()) {
433     return Universe::java_mirror(type());
434   }
435   Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
436   if (klass == NULL) {
437     return NULL;
438   }
439   return has_Q_descriptor() ? InlineKlass::cast(klass)->val_mirror()
440                             : klass->java_mirror();
441 }
442 
443 void SignatureStream::skip_to_return_type() {
444   while (!at_return_type()) {
445     next();
446   }
447 }
448 
449 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
450                                                    Handle class_loader,
451                                                    Handle protection_domain,
452                                                    bool is_method)
453   : SignatureStream(signature, is_method),
454     _class_loader(class_loader), _protection_domain(protection_domain)
455 {
456   initialize_load_origin(NULL);
457 }
458 
459 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
460   : SignatureStream(signature, is_method)

477 
478 void ResolvingSignatureStream::cache_handles() {
479   assert(_load_origin != NULL, "");
480   JavaThread* current = JavaThread::current();
481   _class_loader = Handle(current, _load_origin->class_loader());
482   _protection_domain = Handle(current, _load_origin->protection_domain());
483 }
484 
485 Klass* ResolvingSignatureStream::as_klass_if_loaded(TRAPS) {
486   Klass* klass = as_klass(CachedOrNull, THREAD);
487   // SD::find does not trigger loading, so there should be no throws
488   // Still, bad things can happen, so we CHECK_NULL and ask callers
489   // to do likewise.
490   if (HAS_PENDING_EXCEPTION) {
491     CLEAR_PENDING_EXCEPTION;
492   }
493   return klass;
494 }
495 
496 #ifdef ASSERT

497 extern bool signature_constants_sane(); // called from basic_types_init()
498 
499 bool signature_constants_sane() {
500   // for the lookup table, test every 8-bit code point, and then some:
501   for (int i = -256; i <= 256; i++) {
502     int btcode = 0;
503     switch (i) {
504 #define EACH_SIG(ch, bt, ignore) \
505     case ch: { btcode = bt; break; }
506     SIGNATURE_TYPES_DO(EACH_SIG, ignore)
507 #undef EACH_SIG
508     }
509     int btc = decode_signature_char(i);
510     assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
511   }
512   return true;
513 }
514 
515 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
516   const char* method_sig = (const char*)sig->bytes();
517   ssize_t len = sig->utf8_length();
518   ssize_t index = 0;
519   if (method_sig != NULL && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
520     ++index;
521     while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
522       ssize_t res = is_valid_type(&method_sig[index], len - index);
523       if (res == -1) {
524         return false;
525       } else {
526         index += res;
527       }
528     }
529     if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
530       // check the return type
531       ++index;
532       return (is_valid_type(&method_sig[index], len - index) == (len - index));
533     }
534   }
535   return false;
536 }
537 
538 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
539   const char* type_sig = (const char*)sig->bytes();
540   ssize_t len = sig->utf8_length();
541   return (type_sig != NULL && len >= 1 &&
542           (is_valid_type(type_sig, len) == len));
543 }
544 
545 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
546 // Returns -1 if it is not, or the index of the next character that is not part
547 // of the type.  The type encoding may end before 'limit' and that's ok.
548 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
549   ssize_t index = 0;
550 
551   // Iterate over any number of array dimensions
552   while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
553   if (index >= limit) {
554     return -1;
555   }
556   switch (type[index]) {
557     case JVM_SIGNATURE_BYTE:
558     case JVM_SIGNATURE_CHAR:
559     case JVM_SIGNATURE_FLOAT:
560     case JVM_SIGNATURE_DOUBLE:
561     case JVM_SIGNATURE_INT:
562     case JVM_SIGNATURE_LONG:
563     case JVM_SIGNATURE_SHORT:
564     case JVM_SIGNATURE_BOOLEAN:
565     case JVM_SIGNATURE_VOID:
566       return index + 1;
567     case JVM_SIGNATURE_INLINE_TYPE: // fall through
568     case JVM_SIGNATURE_CLASS:
569       for (index = index + 1; index < limit; ++index) {
570         char c = type[index];
571         switch (c) {
572           case JVM_SIGNATURE_ENDCLASS:
573             return index + 1;
574           case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
575             return -1;
576           default: ; // fall through
577         }
578       }
579       // fall through
580     default: ; // fall through
581   }
582   return -1;
583 }
584 
585 #endif // ASSERT
586 
587 // Adds an argument to the signature
588 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset) {
589   sig->append(SigEntry(bt, offset, symbol));
590   if (bt == T_LONG || bt == T_DOUBLE) {
591     sig->append(SigEntry(T_VOID, offset, symbol)); // Longs and doubles take two stack slots
592   }
593 }
594 
595 // Returns true if the argument at index 'i' is not an inline type delimiter
596 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
597   return (sig->at(i)._bt != T_INLINE_TYPE &&
598           (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
599 }
600 
601 // Fill basic type array from signature array
602 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
603   int count = 0;
604   for (int i = 0; i < sig->length(); i++) {
605     if (skip_value_delimiters(sig, i)) {
606       sig_bt[count++] = sig->at(i)._bt;
607     }
608   }
609   return count;
610 }
611 
612 // Create a temporary symbol from the signature array
613 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
614   ResourceMark rm;
615   int length = sig->length();
616   char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
617   int idx = 0;
618   sig_str[idx++] = '(';
619   for (int i = 0; i < length; i++) {
620     BasicType bt = sig->at(i)._bt;
621     if (bt == T_INLINE_TYPE || bt == T_VOID) {
622       // Ignore
623     } else {
624       if (bt == T_ARRAY) {
625         bt = T_OBJECT; // We don't know the element type, treat as Object
626       }
627       sig_str[idx++] = type2char(bt);
628       if (bt == T_OBJECT) {
629         sig_str[idx++] = ';';
630       }
631     }
632   }
633   sig_str[idx++] = ')';
634   // Add a dummy return type. It won't be used but SignatureStream needs it.
635   sig_str[idx++] = 'V';
636   sig_str[idx++] = '\0';
637   return SymbolTable::new_symbol(sig_str);
638 }
< prev index next >