< prev index next >

src/hotspot/share/runtime/signature.cpp

Print this page

 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;

484   if (name->equals(symbol_chars, len)) {
485     return name;
486   }
487 
488   // Save names for cleaning up reference count at the end of
489   // SignatureStream scope.
490   name = SymbolTable::new_symbol(symbol_chars, len);
491 
492   // Only allocate the GrowableArray for the _names buffer if more than
493   // one name is being processed in the signature.
494   if (!_previous_name->is_permanent()) {
495     if (_names == nullptr) {
496       _names = new GrowableArray<Symbol*>(10);
497     }
498     _names->push(_previous_name);
499   }
500   _previous_name = name;
501   return name;
502 }
503 















504 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
505                                  FailureMode failure_mode, TRAPS) {
506   if (!is_reference()) {
507     return nullptr;
508   }
509   Symbol* name = as_symbol();
510   Klass* k = nullptr;
511   if (failure_mode == ReturnNull) {
512     // Note:  SD::resolve_or_null returns null for most failure modes,
513     // but not all.  Circularity errors, invalid PDs, etc., throw.
514     k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
515   } else if (failure_mode == CachedOrNull) {
516     NoSafepointVerifier nsv;  // no loading, now, we mean it!
517     assert(!HAS_PENDING_EXCEPTION, "");
518     k = SystemDictionary::find_instance_klass(THREAD, name, class_loader, protection_domain);
519     // SD::find does not trigger loading, so there should be no throws
520     // Still, bad things can happen, so we CHECK_NULL and ask callers
521     // to do likewise.
522     return k;
523   } else {

563   : SignatureStream(signature, is_method)
564 {
565   assert(load_origin != nullptr, "");
566   initialize_load_origin(load_origin);
567 }
568 
569 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
570   : SignatureStream(method->signature(), true)
571 {
572   initialize_load_origin(method->method_holder());
573 }
574 
575 void ResolvingSignatureStream::cache_handles() {
576   assert(_load_origin != nullptr, "");
577   JavaThread* current = JavaThread::current();
578   _class_loader = Handle(current, _load_origin->class_loader());
579   _protection_domain = Handle(current, _load_origin->protection_domain());
580 }
581 
582 #ifdef ASSERT
583 
584 extern bool signature_constants_sane(); // called from basic_types_init()
585 
586 bool signature_constants_sane() {
587   // for the lookup table, test every 8-bit code point, and then some:
588   for (int i = -256; i <= 256; i++) {
589     int btcode = 0;
590     switch (i) {
591 #define EACH_SIG(ch, bt, ignore) \
592     case ch: { btcode = bt; break; }
593     SIGNATURE_TYPES_DO(EACH_SIG, ignore)
594 #undef EACH_SIG
595     }
596     int btc = decode_signature_char(i);
597     assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
598   }
599   return true;
600 }
601 
602 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
603   const char* method_sig = (const char*)sig->bytes();
604   ssize_t len = sig->utf8_length();
605   ssize_t index = 0;
606   if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
607     ++index;
608     while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
609       ssize_t res = is_valid_type(&method_sig[index], len - index);
610       if (res == -1) {
611         return false;
612       } else {
613         index += res;
614       }
615     }
616     if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
617       // check the return type
618       ++index;
619       return (is_valid_type(&method_sig[index], len - index) == (len - index));
620     }
621   }
622   return false;
623 }
624 
625 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
626   const char* type_sig = (const char*)sig->bytes();
627   ssize_t len = sig->utf8_length();
628   return (type_sig != nullptr && len >= 1 &&
629           (is_valid_type(type_sig, len) == len));
630 }
631 
632 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
633 // Returns -1 if it is not, or the index of the next character that is not part
634 // of the type.  The type encoding may end before 'limit' and that's ok.
635 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
636   ssize_t index = 0;
637 
638   // Iterate over any number of array dimensions
639   while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
640   if (index >= limit) {
641     return -1;
642   }
643   switch (type[index]) {
644     case JVM_SIGNATURE_BYTE:
645     case JVM_SIGNATURE_CHAR:

652     case JVM_SIGNATURE_VOID:
653       return index + 1;
654     case JVM_SIGNATURE_CLASS:
655       for (index = index + 1; index < limit; ++index) {
656         char c = type[index];
657         switch (c) {
658           case JVM_SIGNATURE_ENDCLASS:
659             return index + 1;
660           case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
661             return -1;
662           default: ; // fall through
663         }
664       }
665       // fall through
666     default: ; // fall through
667   }
668   return -1;
669 }
670 
671 #endif // ASSERT






















































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

486   if (name->equals(symbol_chars, len)) {
487     return name;
488   }
489 
490   // Save names for cleaning up reference count at the end of
491   // SignatureStream scope.
492   name = SymbolTable::new_symbol(symbol_chars, len);
493 
494   // Only allocate the GrowableArray for the _names buffer if more than
495   // one name is being processed in the signature.
496   if (!_previous_name->is_permanent()) {
497     if (_names == nullptr) {
498       _names = new GrowableArray<Symbol*>(10);
499     }
500     _names->push(_previous_name);
501   }
502   _previous_name = name;
503   return name;
504 }
505 
506 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) {
507   ThreadInVMfromUnknown tiv;
508   JavaThread* THREAD = JavaThread::current();
509   HandleMark hm(THREAD);
510   Handle class_loader(THREAD, holder->class_loader());
511   Handle protection_domain(THREAD, holder->protection_domain());
512   Klass* k = as_klass(class_loader, protection_domain, SignatureStream::CachedOrNull, THREAD);
513   assert(!HAS_PENDING_EXCEPTION, "Should never throw");
514   if (k != nullptr && k->is_inline_klass()) {
515     return InlineKlass::cast(k);
516   } else {
517     return nullptr;
518   }
519 }
520 
521 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
522                                  FailureMode failure_mode, TRAPS) {
523   if (!is_reference()) {
524     return nullptr;
525   }
526   Symbol* name = as_symbol();
527   Klass* k = nullptr;
528   if (failure_mode == ReturnNull) {
529     // Note:  SD::resolve_or_null returns null for most failure modes,
530     // but not all.  Circularity errors, invalid PDs, etc., throw.
531     k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
532   } else if (failure_mode == CachedOrNull) {
533     NoSafepointVerifier nsv;  // no loading, now, we mean it!
534     assert(!HAS_PENDING_EXCEPTION, "");
535     k = SystemDictionary::find_instance_klass(THREAD, name, class_loader, protection_domain);
536     // SD::find does not trigger loading, so there should be no throws
537     // Still, bad things can happen, so we CHECK_NULL and ask callers
538     // to do likewise.
539     return k;
540   } else {

580   : SignatureStream(signature, is_method)
581 {
582   assert(load_origin != nullptr, "");
583   initialize_load_origin(load_origin);
584 }
585 
586 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
587   : SignatureStream(method->signature(), true)
588 {
589   initialize_load_origin(method->method_holder());
590 }
591 
592 void ResolvingSignatureStream::cache_handles() {
593   assert(_load_origin != nullptr, "");
594   JavaThread* current = JavaThread::current();
595   _class_loader = Handle(current, _load_origin->class_loader());
596   _protection_domain = Handle(current, _load_origin->protection_domain());
597 }
598 
599 #ifdef ASSERT

600 extern bool signature_constants_sane(); // called from basic_types_init()
601 
602 bool signature_constants_sane() {
603   // for the lookup table, test every 8-bit code point, and then some:
604   for (int i = -256; i <= 256; i++) {
605     int btcode = 0;
606     switch (i) {
607 #define EACH_SIG(ch, bt, ignore) \
608     case ch: { btcode = bt; break; }
609     SIGNATURE_TYPES_DO(EACH_SIG, ignore)
610 #undef EACH_SIG
611     }
612     int btc = decode_signature_char(i);
613     assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
614   }
615   return true;
616 }
617 
618 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
619   const char* method_sig = (const char*)sig->bytes();
620   ssize_t len = sig->utf8_length();
621   ssize_t index = 0;
622   if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
623     ++index;
624     while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
625       ssize_t res = is_valid_type(&method_sig[index], len - index);
626       if (res == -1) {
627         return false;
628       } else {
629         index += res;
630       }
631     }
632     if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
633       // check the return type
634       ++index;
635       return (is_valid_type(&method_sig[index], len - index) == (len - index));
636     }
637   }
638   return false;
639 }
640 
641 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
642   const char* type_sig = (const char*)sig->bytes();
643   ssize_t len = sig->utf8_length();
644   return (type_sig != nullptr && len >= 1 &&
645           (is_valid_type(type_sig, len) == len));
646 }
647 
648 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
649 // Returns -1 if it is not, or the index of the next character that is not part
650 // of the type.  The type encoding may end before 'limit' and that's ok.
651 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
652   ssize_t index = 0;
653 
654   // Iterate over any number of array dimensions
655   while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
656   if (index >= limit) {
657     return -1;
658   }
659   switch (type[index]) {
660     case JVM_SIGNATURE_BYTE:
661     case JVM_SIGNATURE_CHAR:

668     case JVM_SIGNATURE_VOID:
669       return index + 1;
670     case JVM_SIGNATURE_CLASS:
671       for (index = index + 1; index < limit; ++index) {
672         char c = type[index];
673         switch (c) {
674           case JVM_SIGNATURE_ENDCLASS:
675             return index + 1;
676           case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
677             return -1;
678           default: ; // fall through
679         }
680       }
681       // fall through
682     default: ; // fall through
683   }
684   return -1;
685 }
686 
687 #endif // ASSERT
688 
689 // Adds an argument to the signature
690 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset) {
691   sig->append(SigEntry(bt, offset, symbol));
692   if (bt == T_LONG || bt == T_DOUBLE) {
693     sig->append(SigEntry(T_VOID, offset, symbol)); // Longs and doubles take two stack slots
694   }
695 }
696 
697 // Returns true if the argument at index 'i' is not an inline type delimiter
698 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
699   return (sig->at(i)._bt != T_METADATA &&
700           (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
701 }
702 
703 // Fill basic type array from signature array
704 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
705   int count = 0;
706   for (int i = 0; i < sig->length(); i++) {
707     if (skip_value_delimiters(sig, i)) {
708       sig_bt[count++] = sig->at(i)._bt;
709     }
710   }
711   return count;
712 }
713 
714 // Create a temporary symbol from the signature array
715 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
716   ResourceMark rm;
717   int length = sig->length();
718   char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
719   int idx = 0;
720   sig_str[idx++] = '(';
721   for (int i = 0; i < length; i++) {
722     BasicType bt = sig->at(i)._bt;
723     if (bt == T_METADATA || bt == T_VOID) {
724       // Ignore
725     } else {
726       if (bt == T_ARRAY) {
727         bt = T_OBJECT; // We don't know the element type, treat as Object
728       }
729       sig_str[idx++] = type2char(bt);
730       if (bt == T_OBJECT) {
731         sig_str[idx++] = ';';
732       }
733     }
734   }
735   sig_str[idx++] = ')';
736   // Add a dummy return type. It won't be used but SignatureStream needs it.
737   sig_str[idx++] = 'V';
738   sig_str[idx++] = '\0';
739   return SymbolTable::new_symbol(sig_str);
740 }
< prev index next >