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;
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 {
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:
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
|
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;
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 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) {
504 ThreadInVMfromUnknown tiv;
505 JavaThread* THREAD = JavaThread::current();
506 HandleMark hm(THREAD);
507 Handle class_loader(THREAD, holder->class_loader());
508 Handle protection_domain(THREAD, holder->protection_domain());
509 Klass* k = as_klass(class_loader, protection_domain, SignatureStream::CachedOrNull, THREAD);
510 assert(!HAS_PENDING_EXCEPTION, "Should never throw");
511 if (k != nullptr && k->is_inline_klass()) {
512 return InlineKlass::cast(k);
513 } else {
514 return nullptr;
515 }
516 }
517
518 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
519 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, protection_domain, 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, protection_domain);
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 {
577 : SignatureStream(signature, is_method)
578 {
579 assert(load_origin != nullptr, "");
580 initialize_load_origin(load_origin);
581 }
582
583 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
584 : SignatureStream(method->signature(), true)
585 {
586 initialize_load_origin(method->method_holder());
587 }
588
589 void ResolvingSignatureStream::cache_handles() {
590 assert(_load_origin != nullptr, "");
591 JavaThread* current = JavaThread::current();
592 _class_loader = Handle(current, _load_origin->class_loader());
593 _protection_domain = Handle(current, _load_origin->protection_domain());
594 }
595
596 #ifdef ASSERT
597 extern bool signature_constants_sane(); // called from basic_types_init()
598
599 bool signature_constants_sane() {
600 // for the lookup table, test every 8-bit code point, and then some:
601 for (int i = -256; i <= 256; i++) {
602 int btcode = 0;
603 switch (i) {
604 #define EACH_SIG(ch, bt, ignore) \
605 case ch: { btcode = bt; break; }
606 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
607 #undef EACH_SIG
608 }
609 int btc = decode_signature_char(i);
610 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
611 }
612 return true;
613 }
614
615 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
616 const char* method_sig = (const char*)sig->bytes();
617 ssize_t len = sig->utf8_length();
618 ssize_t index = 0;
619 if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
620 ++index;
621 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
622 ssize_t res = is_valid_type(&method_sig[index], len - index);
623 if (res == -1) {
624 return false;
625 } else {
626 index += res;
627 }
628 }
629 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
630 // check the return type
631 ++index;
632 return (is_valid_type(&method_sig[index], len - index) == (len - index));
633 }
634 }
635 return false;
636 }
637
638 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
639 const char* type_sig = (const char*)sig->bytes();
640 ssize_t len = sig->utf8_length();
641 return (type_sig != nullptr && len >= 1 &&
642 (is_valid_type(type_sig, len) == len));
643 }
644
645 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
646 // Returns -1 if it is not, or the index of the next character that is not part
647 // of the type. The type encoding may end before 'limit' and that's ok.
648 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
649 ssize_t index = 0;
650
651 // Iterate over any number of array dimensions
652 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
653 if (index >= limit) {
654 return -1;
655 }
656 switch (type[index]) {
657 case JVM_SIGNATURE_BYTE:
658 case JVM_SIGNATURE_CHAR:
665 case JVM_SIGNATURE_VOID:
666 return index + 1;
667 case JVM_SIGNATURE_CLASS:
668 for (index = index + 1; index < limit; ++index) {
669 char c = type[index];
670 switch (c) {
671 case JVM_SIGNATURE_ENDCLASS:
672 return index + 1;
673 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
674 return -1;
675 default: ; // fall through
676 }
677 }
678 // fall through
679 default: ; // fall through
680 }
681 return -1;
682 }
683
684 #endif // ASSERT
685
686 // Adds an argument to the signature
687 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset) {
688 sig->append(SigEntry(bt, offset, symbol));
689 if (bt == T_LONG || bt == T_DOUBLE) {
690 sig->append(SigEntry(T_VOID, offset, symbol)); // Longs and doubles take two stack slots
691 }
692 }
693
694 // Returns true if the argument at index 'i' is not an inline type delimiter
695 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
696 return (sig->at(i)._bt != T_METADATA &&
697 (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
698 }
699
700 // Fill basic type array from signature array
701 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
702 int count = 0;
703 for (int i = 0; i < sig->length(); i++) {
704 if (skip_value_delimiters(sig, i)) {
705 sig_bt[count++] = sig->at(i)._bt;
706 }
707 }
708 return count;
709 }
710
711 // Create a temporary symbol from the signature array
712 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
713 ResourceMark rm;
714 int length = sig->length();
715 char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
716 int idx = 0;
717 sig_str[idx++] = '(';
718 for (int i = 0; i < length; i++) {
719 BasicType bt = sig->at(i)._bt;
720 if (bt == T_METADATA || bt == T_VOID) {
721 // Ignore
722 } else {
723 if (bt == T_ARRAY) {
724 bt = T_OBJECT; // We don't know the element type, treat as Object
725 }
726 sig_str[idx++] = type2char(bt);
727 if (bt == T_OBJECT) {
728 sig_str[idx++] = ';';
729 }
730 }
731 }
732 sig_str[idx++] = ')';
733 // Add a dummy return type. It won't be used but SignatureStream needs it.
734 sig_str[idx++] = 'V';
735 sig_str[idx++] = '\0';
736 return SymbolTable::new_symbol(sig_str);
737 }
|