< 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   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.

591 #ifdef ASSERT
592 
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* name, int offset, bool null_marker, bool vt_oop) {
684   sig->append(SigEntry(bt, offset, name, null_marker, vt_oop));
685   if (bt == T_LONG || bt == T_DOUBLE) {
686     sig->append(SigEntry(T_VOID, offset, name, false, false)); // Longs and doubles take two stack slots
687   }
688 }
689 void SigEntry::add_null_marker(GrowableArray<SigEntry>* sig, Symbol* name, int offset) {
690   sig->append(SigEntry(T_BOOLEAN, offset, name, true, false));
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 }
737 
738 void SigEntry::print_on(outputStream* st) const {
739   st->print("SigEntry: type=%d offset=%d null_marker=%d ", _bt, _offset, _null_marker);
740   if (_name != nullptr) {
741     _name->print_on(st);
742   } else {
743     st->print("name=nullptr");
744   }
745 }
< prev index next >