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;
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.
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:
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
|
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 "oops/inlineKlass.inline.hpp"
38 #include "runtime/fieldDescriptor.inline.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/interfaceSupport.inline.hpp"
41 #include "runtime/safepointVerifiers.hpp"
42 #include "runtime/sharedRuntime.hpp"
43 #include "runtime/signature.hpp"
44 #include "runtime/sharedRuntime.hpp"
45
46 // Implementation of SignatureIterator
47
48 // Signature syntax:
49 //
50 // Signature = "(" {Parameter} ")" ReturnType.
51 // Parameter = FieldType.
52 // ReturnType = FieldType | "V".
53 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "Q" ValueClassName ";" | "[" FieldType.
54 // ClassName = string.
55
56 // The ClassName string can be any JVM-style UTF8 string except:
57 // - an empty string (the empty string is never a name of any kind)
58 // - a string which begins or ends with slash '/' (the package separator)
59 // - a string which contains adjacent slashes '//' (no empty package names)
60 // - a string which contains a semicolon ';' (the end-delimiter)
61 // - a string which contains a left bracket '[' (the array marker)
62 // - a string which contains a dot '.' (the external package separator)
63 //
64 // Other "meta-looking" characters, such as '(' and '<' and '+',
65 // are perfectly legitimate within a class name, for the JVM.
66 // Class names which contain double slashes ('a//b') and non-initial
67 // brackets ('a[b]') are reserved for possible enrichment of the
68 // type language.
69
70 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
71 if (!fp_is_valid(fingerprint)) {
72 _fingerprint = fingerprint;
73 _return_type = T_ILLEGAL;
485 if (name->equals(symbol_chars, len)) {
486 return name;
487 }
488
489 // Save names for cleaning up reference count at the end of
490 // SignatureStream scope.
491 name = SymbolTable::new_symbol(symbol_chars, len);
492
493 // Only allocate the GrowableArray for the _names buffer if more than
494 // one name is being processed in the signature.
495 if (!_previous_name->is_permanent()) {
496 if (_names == nullptr) {
497 _names = new GrowableArray<Symbol*>(10);
498 }
499 _names->push(_previous_name);
500 }
501 _previous_name = name;
502 return name;
503 }
504
505 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) {
506 ThreadInVMfromUnknown tiv;
507 JavaThread* THREAD = JavaThread::current();
508 HandleMark hm(THREAD);
509 Handle class_loader(THREAD, holder->class_loader());
510 Klass* k = as_klass(class_loader, SignatureStream::CachedOrNull, THREAD);
511 assert(!HAS_PENDING_EXCEPTION, "Should never throw");
512 if (k != nullptr && k->is_inline_klass()) {
513 return InlineKlass::cast(k);
514 } else {
515 return nullptr;
516 }
517 }
518
519 Klass* SignatureStream::as_klass(Handle class_loader, FailureMode failure_mode, TRAPS) {
520 if (!is_reference()) {
521 return nullptr;
522 }
523 Symbol* name = as_symbol();
524 Klass* k = nullptr;
525 if (failure_mode == ReturnNull) {
526 // Note: SD::resolve_or_null returns null for most failure modes,
527 // but not all. Circularity errors, invalid PDs, etc., throw.
528 k = SystemDictionary::resolve_or_null(name, class_loader, CHECK_NULL);
529 } else if (failure_mode == CachedOrNull) {
530 NoSafepointVerifier nsv; // no loading, now, we mean it!
531 assert(!HAS_PENDING_EXCEPTION, "");
532 k = SystemDictionary::find_instance_klass(THREAD, name, class_loader);
533 // SD::find does not trigger loading, so there should be no throws
534 // Still, bad things can happen, so we CHECK_NULL and ask callers
535 // to do likewise.
536 return k;
537 } else {
538 // The only remaining failure mode is NCDFError.
573 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
574 : SignatureStream(signature, is_method)
575 {
576 assert(load_origin != nullptr, "");
577 initialize_load_origin(load_origin);
578 }
579
580 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
581 : SignatureStream(method->signature(), true)
582 {
583 initialize_load_origin(method->method_holder());
584 }
585
586 void ResolvingSignatureStream::cache_handles() {
587 assert(_load_origin != nullptr, "");
588 JavaThread* current = JavaThread::current();
589 _class_loader = Handle(current, _load_origin->class_loader());
590 }
591
592 #ifdef ASSERT
593 extern bool signature_constants_sane(); // called from basic_types_init()
594
595 bool signature_constants_sane() {
596 // for the lookup table, test every 8-bit code point, and then some:
597 for (int i = -256; i <= 256; i++) {
598 int btcode = 0;
599 switch (i) {
600 #define EACH_SIG(ch, bt, ignore) \
601 case ch: { btcode = bt; break; }
602 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
603 #undef EACH_SIG
604 }
605 int btc = decode_signature_char(i);
606 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
607 }
608 return true;
609 }
610
611 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
612 const char* method_sig = (const char*)sig->bytes();
613 ssize_t len = sig->utf8_length();
614 ssize_t index = 0;
615 if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
616 ++index;
617 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
618 ssize_t res = is_valid_type(&method_sig[index], len - index);
619 if (res == -1) {
620 return false;
621 } else {
622 index += res;
623 }
624 }
625 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
626 // check the return type
627 ++index;
628 return (is_valid_type(&method_sig[index], len - index) == (len - index));
629 }
630 }
631 return false;
632 }
633
634 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
635 const char* type_sig = (const char*)sig->bytes();
636 ssize_t len = sig->utf8_length();
637 return (type_sig != nullptr && len >= 1 &&
638 (is_valid_type(type_sig, len) == len));
639 }
640
641 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
642 // Returns -1 if it is not, or the index of the next character that is not part
643 // of the type. The type encoding may end before 'limit' and that's ok.
644 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
645 ssize_t index = 0;
646
647 // Iterate over any number of array dimensions
648 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
649 if (index >= limit) {
650 return -1;
651 }
652 switch (type[index]) {
653 case JVM_SIGNATURE_BYTE:
654 case JVM_SIGNATURE_CHAR:
661 case JVM_SIGNATURE_VOID:
662 return index + 1;
663 case JVM_SIGNATURE_CLASS:
664 for (index = index + 1; index < limit; ++index) {
665 char c = type[index];
666 switch (c) {
667 case JVM_SIGNATURE_ENDCLASS:
668 return index + 1;
669 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
670 return -1;
671 default: ; // fall through
672 }
673 }
674 // fall through
675 default: ; // fall through
676 }
677 return -1;
678 }
679
680 #endif // ASSERT
681
682 // Adds an argument to the signature
683 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset, float sort_offset) {
684 if (sort_offset == -1) {
685 sort_offset = offset;
686 }
687 sig->append(SigEntry(bt, offset, sort_offset, symbol));
688 if (bt == T_LONG || bt == T_DOUBLE) {
689 sig->append(SigEntry(T_VOID, offset, sort_offset, symbol)); // Longs and doubles take two stack slots
690 }
691 }
692
693 // Returns true if the argument at index 'i' is not an inline type delimiter
694 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
695 return (sig->at(i)._bt != T_METADATA &&
696 (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
697 }
698
699 // Fill basic type array from signature array
700 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
701 int count = 0;
702 for (int i = 0; i < sig->length(); i++) {
703 if (skip_value_delimiters(sig, i)) {
704 sig_bt[count++] = sig->at(i)._bt;
705 }
706 }
707 return count;
708 }
709
710 // Create a temporary symbol from the signature array
711 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
712 ResourceMark rm;
713 int length = sig->length();
714 char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
715 int idx = 0;
716 sig_str[idx++] = '(';
717 for (int i = 0; i < length; i++) {
718 BasicType bt = sig->at(i)._bt;
719 if (bt == T_METADATA || bt == T_VOID) {
720 // Ignore
721 } else {
722 if (bt == T_ARRAY) {
723 bt = T_OBJECT; // We don't know the element type, treat as Object
724 }
725 sig_str[idx++] = type2char(bt);
726 if (bt == T_OBJECT) {
727 sig_str[idx++] = ';';
728 }
729 }
730 }
731 sig_str[idx++] = ')';
732 // Add a dummy return type. It won't be used but SignatureStream needs it.
733 sig_str[idx++] = 'V';
734 sig_str[idx++] = '\0';
735 return SymbolTable::new_symbol(sig_str);
736 }
|