< prev index next >

src/hotspot/share/runtime/signature.cpp

Print this page

  1 /*
  2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 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)

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.

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


































































  1 /*
  2  * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 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 ";" | "[" 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)

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   assert(InlineTypePassFieldsAsArgs || InlineTypeReturnedAsFields, "Not needed");
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.

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

662     case JVM_SIGNATURE_VOID:
663       return index + 1;
664     case JVM_SIGNATURE_CLASS:
665       for (index = index + 1; index < limit; ++index) {
666         char c = type[index];
667         switch (c) {
668           case JVM_SIGNATURE_ENDCLASS:
669             return index + 1;
670           case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
671             return -1;
672           default: ; // fall through
673         }
674       }
675       // fall through
676     default: ; // fall through
677   }
678   return -1;
679 }
680 
681 #endif // ASSERT
682 
683 // Adds an argument to the signature
684 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* name, int offset, bool null_marker, bool vt_oop) {
685   sig->append(SigEntry(bt, offset, name, null_marker, vt_oop));
686   if (bt == T_LONG || bt == T_DOUBLE) {
687     sig->append(SigEntry(T_VOID, offset, name, false, false)); // Longs and doubles take two stack slots
688   }
689 }
690 void SigEntry::add_null_marker(GrowableArray<SigEntry>* sig, Symbol* name, int offset) {
691   sig->append(SigEntry(T_BOOLEAN, offset, name, true, false));
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 }
738 
739 void SigEntry::print_on(outputStream* st) const {
740   st->print("SigEntry: type=%d offset=%d null_marker=%d ", _bt, _offset, _null_marker);
741   if (_name != nullptr) {
742     _name->print_on(st);
743   } else {
744     st->print("name=nullptr");
745   }
746 }
< prev index next >