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 "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
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 ";" | "[" 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;
482 if (name->equals(symbol_chars, len)) {
483 return name;
484 }
485
486 // Save names for cleaning up reference count at the end of
487 // SignatureStream scope.
488 name = SymbolTable::new_symbol(symbol_chars, len);
489
490 // Only allocate the GrowableArray for the _names buffer if more than
491 // one name is being processed in the signature.
492 if (!_previous_name->is_permanent()) {
493 if (_names == nullptr) {
494 _names = new GrowableArray<Symbol*>(10);
495 }
496 _names->push(_previous_name);
497 }
498 _previous_name = name;
499 return name;
500 }
501
502 Klass* SignatureStream::as_klass(Handle class_loader, 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, 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);
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.
556 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
557 : SignatureStream(signature, is_method)
558 {
559 assert(load_origin != nullptr, "");
560 initialize_load_origin(load_origin);
561 }
562
563 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
564 : SignatureStream(method->signature(), true)
565 {
566 initialize_load_origin(method->method_holder());
567 }
568
569 void ResolvingSignatureStream::cache_handles() {
570 assert(_load_origin != nullptr, "");
571 JavaThread* current = JavaThread::current();
572 _class_loader = Handle(current, _load_origin->class_loader());
573 }
574
575 #ifdef ASSERT
576
577 extern bool signature_constants_sane(); // called from basic_types_init()
578
579 bool signature_constants_sane() {
580 // for the lookup table, test every 8-bit code point, and then some:
581 for (int i = -256; i <= 256; i++) {
582 int btcode = 0;
583 switch (i) {
584 #define EACH_SIG(ch, bt, ignore) \
585 case ch: { btcode = bt; break; }
586 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
587 #undef EACH_SIG
588 }
589 int btc = decode_signature_char(i);
590 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
591 }
592 return true;
593 }
594
595 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
596 const char* method_sig = (const char*)sig->bytes();
597 ssize_t len = sig->utf8_length();
598 ssize_t index = 0;
599 if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
600 ++index;
601 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
602 ssize_t res = is_valid_type(&method_sig[index], len - index);
603 if (res == -1) {
604 return false;
605 } else {
606 index += res;
607 }
608 }
609 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
610 // check the return type
611 ++index;
612 return (is_valid_type(&method_sig[index], len - index) == (len - index));
613 }
614 }
615 return false;
616 }
617
618 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
619 const char* type_sig = (const char*)sig->bytes();
620 ssize_t len = sig->utf8_length();
621 return (type_sig != nullptr && len >= 1 &&
622 (is_valid_type(type_sig, len) == len));
623 }
624
625 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
626 // Returns -1 if it is not, or the index of the next character that is not part
627 // of the type. The type encoding may end before 'limit' and that's ok.
628 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
629 ssize_t index = 0;
630
631 // Iterate over any number of array dimensions
632 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
633 if (index >= limit) {
634 return -1;
635 }
636 switch (type[index]) {
637 case JVM_SIGNATURE_BYTE:
638 case JVM_SIGNATURE_CHAR:
645 case JVM_SIGNATURE_VOID:
646 return index + 1;
647 case JVM_SIGNATURE_CLASS:
648 for (index = index + 1; index < limit; ++index) {
649 char c = type[index];
650 switch (c) {
651 case JVM_SIGNATURE_ENDCLASS:
652 return index + 1;
653 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
654 return -1;
655 default: ; // fall through
656 }
657 }
658 // fall through
659 default: ; // fall through
660 }
661 return -1;
662 }
663
664 #endif // ASSERT
|
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 "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/inlineKlass.inline.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/interfaceSupport.inline.hpp"
41 #include "runtime/safepointVerifiers.hpp"
42 #include "runtime/sharedRuntime.hpp"
43 #include "runtime/signature.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 ";" | "Q" ValueClassName ";" | "[" 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 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) {
505 ThreadInVMfromUnknown tiv;
506 JavaThread* THREAD = JavaThread::current();
507 HandleMark hm(THREAD);
508 Handle class_loader(THREAD, holder->class_loader());
509 Klass* k = as_klass(class_loader, 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, FailureMode failure_mode, TRAPS) {
519 if (!is_reference()) {
520 return nullptr;
521 }
522 Symbol* name = as_symbol();
523 Klass* k = nullptr;
524 if (failure_mode == ReturnNull) {
525 // Note: SD::resolve_or_null returns null for most failure modes,
526 // but not all. Circularity errors, invalid PDs, etc., throw.
527 k = SystemDictionary::resolve_or_null(name, class_loader, CHECK_NULL);
528 } else if (failure_mode == CachedOrNull) {
529 NoSafepointVerifier nsv; // no loading, now, we mean it!
530 assert(!HAS_PENDING_EXCEPTION, "");
531 k = SystemDictionary::find_instance_klass(THREAD, name, class_loader);
532 // SD::find does not trigger loading, so there should be no throws
533 // Still, bad things can happen, so we CHECK_NULL and ask callers
534 // to do likewise.
535 return k;
536 } else {
537 // The only remaining failure mode is NCDFError.
572 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
573 : SignatureStream(signature, is_method)
574 {
575 assert(load_origin != nullptr, "");
576 initialize_load_origin(load_origin);
577 }
578
579 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
580 : SignatureStream(method->signature(), true)
581 {
582 initialize_load_origin(method->method_holder());
583 }
584
585 void ResolvingSignatureStream::cache_handles() {
586 assert(_load_origin != nullptr, "");
587 JavaThread* current = JavaThread::current();
588 _class_loader = Handle(current, _load_origin->class_loader());
589 }
590
591 #ifdef ASSERT
592 extern bool signature_constants_sane(); // called from basic_types_init()
593
594 bool signature_constants_sane() {
595 // for the lookup table, test every 8-bit code point, and then some:
596 for (int i = -256; i <= 256; i++) {
597 int btcode = 0;
598 switch (i) {
599 #define EACH_SIG(ch, bt, ignore) \
600 case ch: { btcode = bt; break; }
601 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
602 #undef EACH_SIG
603 }
604 int btc = decode_signature_char(i);
605 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
606 }
607 return true;
608 }
609
610 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
611 const char* method_sig = (const char*)sig->bytes();
612 ssize_t len = sig->utf8_length();
613 ssize_t index = 0;
614 if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
615 ++index;
616 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
617 ssize_t res = is_valid_type(&method_sig[index], len - index);
618 if (res == -1) {
619 return false;
620 } else {
621 index += res;
622 }
623 }
624 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
625 // check the return type
626 ++index;
627 return (is_valid_type(&method_sig[index], len - index) == (len - index));
628 }
629 }
630 return false;
631 }
632
633 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
634 const char* type_sig = (const char*)sig->bytes();
635 ssize_t len = sig->utf8_length();
636 return (type_sig != nullptr && len >= 1 &&
637 (is_valid_type(type_sig, len) == len));
638 }
639
640 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
641 // Returns -1 if it is not, or the index of the next character that is not part
642 // of the type. The type encoding may end before 'limit' and that's ok.
643 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
644 ssize_t index = 0;
645
646 // Iterate over any number of array dimensions
647 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
648 if (index >= limit) {
649 return -1;
650 }
651 switch (type[index]) {
652 case JVM_SIGNATURE_BYTE:
653 case JVM_SIGNATURE_CHAR:
660 case JVM_SIGNATURE_VOID:
661 return index + 1;
662 case JVM_SIGNATURE_CLASS:
663 for (index = index + 1; index < limit; ++index) {
664 char c = type[index];
665 switch (c) {
666 case JVM_SIGNATURE_ENDCLASS:
667 return index + 1;
668 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
669 return -1;
670 default: ; // fall through
671 }
672 }
673 // fall through
674 default: ; // fall through
675 }
676 return -1;
677 }
678
679 #endif // ASSERT
680
681 // Adds an argument to the signature
682 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* name, int offset) {
683 sig->append(SigEntry(bt, offset, name, false));
684 if (bt == T_LONG || bt == T_DOUBLE) {
685 sig->append(SigEntry(T_VOID, offset, name, false)); // Longs and doubles take two stack slots
686 }
687 }
688 void SigEntry::add_null_marker(GrowableArray<SigEntry>* sig, Symbol* name, int offset) {
689 sig->append(SigEntry(T_BOOLEAN, offset, name, true));
690 }
691
692 // Returns true if the argument at index 'i' is not an inline type delimiter
693 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
694 return (sig->at(i)._bt != T_METADATA &&
695 (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
696 }
697
698 // Fill basic type array from signature array
699 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
700 int count = 0;
701 for (int i = 0; i < sig->length(); i++) {
702 if (skip_value_delimiters(sig, i)) {
703 sig_bt[count++] = sig->at(i)._bt;
704 }
705 }
706 return count;
707 }
708
709 // Create a temporary symbol from the signature array
710 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
711 ResourceMark rm;
712 int length = sig->length();
713 char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
714 int idx = 0;
715 sig_str[idx++] = '(';
716 for (int i = 0; i < length; i++) {
717 BasicType bt = sig->at(i)._bt;
718 if (bt == T_METADATA || bt == T_VOID) {
719 // Ignore
720 } else {
721 if (bt == T_ARRAY) {
722 bt = T_OBJECT; // We don't know the element type, treat as Object
723 }
724 sig_str[idx++] = type2char(bt);
725 if (bt == T_OBJECT) {
726 sig_str[idx++] = ';';
727 }
728 }
729 }
730 sig_str[idx++] = ')';
731 // Add a dummy return type. It won't be used but SignatureStream needs it.
732 sig_str[idx++] = 'V';
733 sig_str[idx++] = '\0';
734 return SymbolTable::new_symbol(sig_str);
735 }
|