< prev index next >

src/hotspot/share/runtime/signature.hpp

Print this page

  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_RUNTIME_SIGNATURE_HPP
 27 #define SHARE_RUNTIME_SIGNATURE_HPP
 28 

 29 #include "memory/allocation.hpp"
 30 #include "oops/method.hpp"
 31 
 32 
 33 // Static routines and parsing loops for processing field and method
 34 // descriptors.  In the HotSpot sources we call them "signatures".
 35 //
 36 // A SignatureStream iterates over a Java descriptor (or parts of it).
 37 // The syntax is documented in the Java Virtual Machine Specification,
 38 // section 4.3.
 39 //
 40 // The syntax may be summarized as follows:
 41 //
 42 //     MethodType: '(' {FieldType}* ')' (FieldType | 'V')
 43 //     FieldType: PrimitiveType | ObjectType | ArrayType
 44 //     PrimitiveType: 'B' | 'C' | 'D' | 'F' | 'I' | 'J' | 'S' | 'Z'
 45 //     ObjectType: 'L' ClassName ';' | ArrayType
 46 //     ArrayType: '[' FieldType
 47 //     ClassName: {UnqualifiedName '/'}* UnqualifiedName
 48 //     UnqualifiedName: NameChar {NameChar}*

107 
108   // Assuming it is either a class name or signature,
109   // determine if it in fact is an array descriptor.
110   static bool is_array(const Symbol* signature) {
111     return (signature->utf8_length() > 1 &&
112             signature->char_at(0) == JVM_SIGNATURE_ARRAY &&
113             is_valid_array_signature(signature));
114   }
115 
116   // Assuming it is either a class name or signature,
117   // determine if it contains a class name plus ';'.
118   static bool has_envelope(const Symbol* signature) {
119     return ((signature->utf8_length() > 0) &&
120             signature->ends_with(JVM_SIGNATURE_ENDCLASS) &&
121             has_envelope(signature->char_at(0)));
122   }
123 
124   // Determine if this signature char introduces an
125   // envelope, which is a class name plus ';'.
126   static bool has_envelope(char signature_char) {
127     return (signature_char == JVM_SIGNATURE_CLASS);
128   }
129 
130   // Assuming has_envelope is true, return the symbol
131   // inside the envelope, by stripping 'L' and ';'.
132   // Caller is responsible for decrementing the newly created
133   // Symbol's refcount, use TempNewSymbol.
134   static Symbol* strip_envelope(const Symbol* signature);
135 
136   // Assuming it's either a field or method descriptor, determine
137   // whether it is in fact a method descriptor:
138   static bool is_method(const Symbol* signature) {
139     return signature->starts_with(JVM_SIGNATURE_FUNC);
140   }
141 
142   // Assuming it's a method signature, determine if it must
143   // return void.
144   static bool is_void_method(const Symbol* signature) {
145     assert(is_method(signature), "signature is not for a method");
146     return signature->ends_with(JVM_SIGNATURE_VOID);
147   }

252 
253 // Specialized SignatureIterators: Used to compute signature specific values.
254 
255 class SignatureTypeNames : public SignatureIterator {
256  protected:
257   virtual void type_name(const char* name)   = 0;
258 
259   friend class SignatureIterator;  // so do_parameters_on can call do_type
260   void do_type(BasicType type) {
261     switch (type) {
262     case T_BOOLEAN: type_name("jboolean"); break;
263     case T_CHAR:    type_name("jchar"   ); break;
264     case T_FLOAT:   type_name("jfloat"  ); break;
265     case T_DOUBLE:  type_name("jdouble" ); break;
266     case T_BYTE:    type_name("jbyte"   ); break;
267     case T_SHORT:   type_name("jshort"  ); break;
268     case T_INT:     type_name("jint"    ); break;
269     case T_LONG:    type_name("jlong"   ); break;
270     case T_VOID:    type_name("void"    ); break;
271     case T_ARRAY:
272     case T_OBJECT:  type_name("jobject" ); break;

273     default: ShouldNotReachHere();
274     }
275   }
276 
277  public:
278   SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {}
279 };
280 
281 
282 // Specialized SignatureIterator: Used to compute the argument size.
283 
284 class ArgumentSizeComputer: public SignatureIterator {
285  private:
286   int _size;
287   friend class SignatureIterator;  // so do_parameters_on can call do_type
288   void do_type(BasicType type) { _size += parameter_type_word_count(type); }
289  public:
290   ArgumentSizeComputer(Symbol* signature);
291   int size() { return _size; }
292 };

390       pass_short();  _jni_offset++; _offset++;
391       break;
392     case T_INT:
393       pass_int();    _jni_offset++; _offset++;
394       break;
395     case T_FLOAT:
396       pass_float();  _jni_offset++; _offset++;
397       break;
398     case T_DOUBLE: {
399       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
400       pass_double(); _jni_offset += jni_offset; _offset += 2;
401       break;
402     }
403     case T_LONG: {
404       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
405       pass_long();   _jni_offset += jni_offset; _offset += 2;
406       break;
407     }
408     case T_ARRAY:
409     case T_OBJECT:

410       pass_object(); _jni_offset++; _offset++;
411       break;
412     default:
413       ShouldNotReachHere();
414     }
415   }
416 
417  public:
418   methodHandle method() const          { return _method; }
419   int          offset() const          { return _offset; }
420   int      jni_offset() const          { return _jni_offset + _prepended; }
421   bool      is_static() const          { return method()->is_static(); }
422   virtual void pass_int()              = 0;
423   virtual void pass_long()             = 0;
424   virtual void pass_object()           = 0;  // objects, arrays, inlines
425   virtual void pass_float()            = 0;
426   virtual void pass_byte()             { pass_int(); };
427   virtual void pass_short()            { pass_int(); };
428 #ifdef _LP64
429   virtual void pass_double()           = 0;

503 
504   const u1* raw_bytes() const  { return _signature->bytes() + _begin; }
505   int       raw_length() const { return _end - _begin; }
506   int raw_symbol_begin() const { return _begin + (has_envelope() ? 1 : 0); }
507   int raw_symbol_end() const   { return _end  -  (has_envelope() ? 1 : 0); }
508   char raw_char_at(int i) const {
509     assert(i < _limit, "index for raw_char_at is over the limit");
510     return _signature->char_at(i);
511   }
512 
513   // True if there is an embedded class name in this type,
514   // followed by ';'.
515   bool has_envelope() const {
516     if (!Signature::has_envelope(_signature->char_at(_begin)))
517       return false;
518     // this should always be true, but let's test it:
519     assert(_signature->char_at(_end-1) == JVM_SIGNATURE_ENDCLASS, "signature envelope has no semi-colon at end");
520     return true;
521   }
522 




523   // return the symbol for chars in symbol_begin()..symbol_end()
524   Symbol* as_symbol() {
525     return find_symbol();
526   }
527 
528   // in case you want only the return type:
529   void skip_to_return_type();
530 
531   // number of '[' in array prefix
532   int array_prefix_length() {
533     return _type == T_ARRAY ? _array_prefix : 0;
534   }
535 
536   // In case you want only the array base type,
537   // reset the stream after skipping some brackets '['.
538   // (The argument is clipped to array_prefix_length(),
539   // and if it ends up as zero this call is a nop.
540   // The default is value skips all brackets '['.)
541  private:
542   int skip_whole_array_prefix();

545     if (_type != T_ARRAY) {
546       return 0;
547     }
548      if (_array_prefix > max_skip_length) {
549       // strip some but not all levels of T_ARRAY
550       _array_prefix -= max_skip_length;
551       _begin += max_skip_length;
552       return max_skip_length;
553     }
554     return skip_whole_array_prefix();
555   }
556   int skip_array_prefix() {
557     if (_type != T_ARRAY) {
558       return 0;
559     }
560     return skip_whole_array_prefix();
561   }
562 
563   // free-standing lookups (bring your own CL/PD pair)
564   enum FailureMode { ReturnNull, NCDFError, CachedOrNull };

565   Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);

566   oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
567 };
568 



















































569 // Specialized SignatureStream: used for invoking SystemDictionary to either find
570 //                              or resolve the underlying type when iterating over a
571 //                              Java descriptor (or parts of it).
572 class ResolvingSignatureStream : public SignatureStream {
573   Klass*       _load_origin;
574   bool         _handles_cached;
575   Handle       _class_loader;       // cached when needed
576   Handle       _protection_domain;  // cached when needed
577 
578   void initialize_load_origin(Klass* load_origin) {
579     _load_origin = load_origin;
580     _handles_cached = (load_origin == NULL);
581   }
582   void need_handles() {
583     if (!_handles_cached) {
584       cache_handles();
585       _handles_cached = true;
586     }
587   }
588   void cache_handles();

620 
621   // Check for too many arguments, or missing fingerprint:
622   if (!fp_is_valid(unaccumulator)) {
623     SignatureStream ss(_signature);
624     for (; !ss.at_return_type(); ss.next()) {
625       callback->do_type(ss.type());
626     }
627     // while we are here, capture the return type
628     _return_type = ss.type();
629   } else {
630     // Optimized version of do_parameters when fingerprint is known
631     assert(_return_type != T_ILLEGAL, "return type already captured from fp");
632     unaccumulator = fp_start_parameters(unaccumulator);
633     for (BasicType type; (type = fp_next_parameter(unaccumulator)) != (BasicType)fp_parameters_done; ) {
634       assert(fp_is_valid_type(type), "garbled fingerprint");
635       callback->do_type(type);
636     }
637   }
638 }
639 
640  #ifdef ASSERT
641  class SignatureVerifier : public StackObj {
642   public:
643     static bool is_valid_method_signature(Symbol* sig);
644     static bool is_valid_type_signature(Symbol* sig);
645   private:
646     static ssize_t is_valid_type(const char*, ssize_t);
647 };
648 #endif
649 #endif // SHARE_RUNTIME_SIGNATURE_HPP

  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_RUNTIME_SIGNATURE_HPP
 27 #define SHARE_RUNTIME_SIGNATURE_HPP
 28 
 29 #include "classfile/symbolTable.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "oops/method.hpp"
 32 
 33 
 34 // Static routines and parsing loops for processing field and method
 35 // descriptors.  In the HotSpot sources we call them "signatures".
 36 //
 37 // A SignatureStream iterates over a Java descriptor (or parts of it).
 38 // The syntax is documented in the Java Virtual Machine Specification,
 39 // section 4.3.
 40 //
 41 // The syntax may be summarized as follows:
 42 //
 43 //     MethodType: '(' {FieldType}* ')' (FieldType | 'V')
 44 //     FieldType: PrimitiveType | ObjectType | ArrayType
 45 //     PrimitiveType: 'B' | 'C' | 'D' | 'F' | 'I' | 'J' | 'S' | 'Z'
 46 //     ObjectType: 'L' ClassName ';' | ArrayType
 47 //     ArrayType: '[' FieldType
 48 //     ClassName: {UnqualifiedName '/'}* UnqualifiedName
 49 //     UnqualifiedName: NameChar {NameChar}*

108 
109   // Assuming it is either a class name or signature,
110   // determine if it in fact is an array descriptor.
111   static bool is_array(const Symbol* signature) {
112     return (signature->utf8_length() > 1 &&
113             signature->char_at(0) == JVM_SIGNATURE_ARRAY &&
114             is_valid_array_signature(signature));
115   }
116 
117   // Assuming it is either a class name or signature,
118   // determine if it contains a class name plus ';'.
119   static bool has_envelope(const Symbol* signature) {
120     return ((signature->utf8_length() > 0) &&
121             signature->ends_with(JVM_SIGNATURE_ENDCLASS) &&
122             has_envelope(signature->char_at(0)));
123   }
124 
125   // Determine if this signature char introduces an
126   // envelope, which is a class name plus ';'.
127   static bool has_envelope(char signature_char) {
128     return (signature_char == JVM_SIGNATURE_CLASS) || (signature_char == JVM_SIGNATURE_INLINE_TYPE);
129   }
130 
131   // Assuming has_envelope is true, return the symbol
132   // inside the envelope, by stripping 'L' and ';'.
133   // Caller is responsible for decrementing the newly created
134   // Symbol's refcount, use TempNewSymbol.
135   static Symbol* strip_envelope(const Symbol* signature);
136 
137   // Assuming it's either a field or method descriptor, determine
138   // whether it is in fact a method descriptor:
139   static bool is_method(const Symbol* signature) {
140     return signature->starts_with(JVM_SIGNATURE_FUNC);
141   }
142 
143   // Assuming it's a method signature, determine if it must
144   // return void.
145   static bool is_void_method(const Symbol* signature) {
146     assert(is_method(signature), "signature is not for a method");
147     return signature->ends_with(JVM_SIGNATURE_VOID);
148   }

253 
254 // Specialized SignatureIterators: Used to compute signature specific values.
255 
256 class SignatureTypeNames : public SignatureIterator {
257  protected:
258   virtual void type_name(const char* name)   = 0;
259 
260   friend class SignatureIterator;  // so do_parameters_on can call do_type
261   void do_type(BasicType type) {
262     switch (type) {
263     case T_BOOLEAN: type_name("jboolean"); break;
264     case T_CHAR:    type_name("jchar"   ); break;
265     case T_FLOAT:   type_name("jfloat"  ); break;
266     case T_DOUBLE:  type_name("jdouble" ); break;
267     case T_BYTE:    type_name("jbyte"   ); break;
268     case T_SHORT:   type_name("jshort"  ); break;
269     case T_INT:     type_name("jint"    ); break;
270     case T_LONG:    type_name("jlong"   ); break;
271     case T_VOID:    type_name("void"    ); break;
272     case T_ARRAY:
273     case T_OBJECT:
274     case T_INLINE_TYPE:  type_name("jobject" ); break;
275     default: ShouldNotReachHere();
276     }
277   }
278 
279  public:
280   SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {}
281 };
282 
283 
284 // Specialized SignatureIterator: Used to compute the argument size.
285 
286 class ArgumentSizeComputer: public SignatureIterator {
287  private:
288   int _size;
289   friend class SignatureIterator;  // so do_parameters_on can call do_type
290   void do_type(BasicType type) { _size += parameter_type_word_count(type); }
291  public:
292   ArgumentSizeComputer(Symbol* signature);
293   int size() { return _size; }
294 };

392       pass_short();  _jni_offset++; _offset++;
393       break;
394     case T_INT:
395       pass_int();    _jni_offset++; _offset++;
396       break;
397     case T_FLOAT:
398       pass_float();  _jni_offset++; _offset++;
399       break;
400     case T_DOUBLE: {
401       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
402       pass_double(); _jni_offset += jni_offset; _offset += 2;
403       break;
404     }
405     case T_LONG: {
406       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
407       pass_long();   _jni_offset += jni_offset; _offset += 2;
408       break;
409     }
410     case T_ARRAY:
411     case T_OBJECT:
412     case T_INLINE_TYPE:
413       pass_object(); _jni_offset++; _offset++;
414       break;
415     default:
416       ShouldNotReachHere();
417     }
418   }
419 
420  public:
421   methodHandle method() const          { return _method; }
422   int          offset() const          { return _offset; }
423   int      jni_offset() const          { return _jni_offset + _prepended; }
424   bool      is_static() const          { return method()->is_static(); }
425   virtual void pass_int()              = 0;
426   virtual void pass_long()             = 0;
427   virtual void pass_object()           = 0;  // objects, arrays, inlines
428   virtual void pass_float()            = 0;
429   virtual void pass_byte()             { pass_int(); };
430   virtual void pass_short()            { pass_int(); };
431 #ifdef _LP64
432   virtual void pass_double()           = 0;

506 
507   const u1* raw_bytes() const  { return _signature->bytes() + _begin; }
508   int       raw_length() const { return _end - _begin; }
509   int raw_symbol_begin() const { return _begin + (has_envelope() ? 1 : 0); }
510   int raw_symbol_end() const   { return _end  -  (has_envelope() ? 1 : 0); }
511   char raw_char_at(int i) const {
512     assert(i < _limit, "index for raw_char_at is over the limit");
513     return _signature->char_at(i);
514   }
515 
516   // True if there is an embedded class name in this type,
517   // followed by ';'.
518   bool has_envelope() const {
519     if (!Signature::has_envelope(_signature->char_at(_begin)))
520       return false;
521     // this should always be true, but let's test it:
522     assert(_signature->char_at(_end-1) == JVM_SIGNATURE_ENDCLASS, "signature envelope has no semi-colon at end");
523     return true;
524   }
525 
526   bool has_Q_descriptor() const {
527     return has_envelope() && (_signature->char_at(_begin) == JVM_SIGNATURE_INLINE_TYPE);
528   }
529 
530   // return the symbol for chars in symbol_begin()..symbol_end()
531   Symbol* as_symbol() {
532     return find_symbol();
533   }
534 
535   // in case you want only the return type:
536   void skip_to_return_type();
537 
538   // number of '[' in array prefix
539   int array_prefix_length() {
540     return _type == T_ARRAY ? _array_prefix : 0;
541   }
542 
543   // In case you want only the array base type,
544   // reset the stream after skipping some brackets '['.
545   // (The argument is clipped to array_prefix_length(),
546   // and if it ends up as zero this call is a nop.
547   // The default is value skips all brackets '['.)
548  private:
549   int skip_whole_array_prefix();

552     if (_type != T_ARRAY) {
553       return 0;
554     }
555      if (_array_prefix > max_skip_length) {
556       // strip some but not all levels of T_ARRAY
557       _array_prefix -= max_skip_length;
558       _begin += max_skip_length;
559       return max_skip_length;
560     }
561     return skip_whole_array_prefix();
562   }
563   int skip_array_prefix() {
564     if (_type != T_ARRAY) {
565       return 0;
566     }
567     return skip_whole_array_prefix();
568   }
569 
570   // free-standing lookups (bring your own CL/PD pair)
571   enum FailureMode { ReturnNull, NCDFError, CachedOrNull };
572 
573   Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
574   InlineKlass* as_inline_klass(InstanceKlass* holder);
575   oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
576 };
577 
578 class SigEntryFilter;
579 typedef GrowableArrayFilterIterator<SigEntry, SigEntryFilter> ExtendedSignature;
580 
581 // Used for adapter generation. One SigEntry is used per element of
582 // the signature of the method. Inline type arguments are treated
583 // specially. See comment for InlineKlass::collect_fields().
584 class SigEntry {
585  public:
586   BasicType _bt;
587   int _offset;
588   Symbol* _symbol;
589 
590   SigEntry()
591     : _bt(T_ILLEGAL), _offset(-1), _symbol(NULL) {}
592 
593   SigEntry(BasicType bt, int offset, Symbol* symbol)
594     : _bt(bt), _offset(offset), _symbol(symbol) {}
595 
596   static int compare(SigEntry* e1, SigEntry* e2) {
597     if (e1->_offset != e2->_offset) {
598       return e1->_offset - e2->_offset;
599     }
600     assert((e1->_bt == T_LONG && (e2->_bt == T_LONG || e2->_bt == T_VOID)) ||
601            (e1->_bt == T_DOUBLE && (e2->_bt == T_DOUBLE || e2->_bt == T_VOID)) ||
602            e1->_bt == T_INLINE_TYPE || e2->_bt == T_INLINE_TYPE || e1->_bt == T_VOID || e2->_bt == T_VOID, "bad bt");
603     if (e1->_bt == e2->_bt) {
604       assert(e1->_bt == T_INLINE_TYPE || e1->_bt == T_VOID, "only ones with duplicate offsets");
605       return 0;
606     }
607     if (e1->_bt == T_VOID ||
608         e2->_bt == T_INLINE_TYPE) {
609       return 1;
610     }
611     if (e1->_bt == T_INLINE_TYPE ||
612         e2->_bt == T_VOID) {
613       return -1;
614     }
615     ShouldNotReachHere();
616     return 0;
617   }
618   static void add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset = -1);
619   static bool skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i);
620   static int fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt);
621   static TempNewSymbol create_symbol(const GrowableArray<SigEntry>* sig);
622 };
623 
624 class SigEntryFilter {
625 public:
626   bool operator()(const SigEntry& entry) { return entry._bt != T_INLINE_TYPE && entry._bt != T_VOID; }
627 };
628 
629 // Specialized SignatureStream: used for invoking SystemDictionary to either find
630 //                              or resolve the underlying type when iterating over a
631 //                              Java descriptor (or parts of it).
632 class ResolvingSignatureStream : public SignatureStream {
633   Klass*       _load_origin;
634   bool         _handles_cached;
635   Handle       _class_loader;       // cached when needed
636   Handle       _protection_domain;  // cached when needed
637 
638   void initialize_load_origin(Klass* load_origin) {
639     _load_origin = load_origin;
640     _handles_cached = (load_origin == NULL);
641   }
642   void need_handles() {
643     if (!_handles_cached) {
644       cache_handles();
645       _handles_cached = true;
646     }
647   }
648   void cache_handles();

680 
681   // Check for too many arguments, or missing fingerprint:
682   if (!fp_is_valid(unaccumulator)) {
683     SignatureStream ss(_signature);
684     for (; !ss.at_return_type(); ss.next()) {
685       callback->do_type(ss.type());
686     }
687     // while we are here, capture the return type
688     _return_type = ss.type();
689   } else {
690     // Optimized version of do_parameters when fingerprint is known
691     assert(_return_type != T_ILLEGAL, "return type already captured from fp");
692     unaccumulator = fp_start_parameters(unaccumulator);
693     for (BasicType type; (type = fp_next_parameter(unaccumulator)) != (BasicType)fp_parameters_done; ) {
694       assert(fp_is_valid_type(type), "garbled fingerprint");
695       callback->do_type(type);
696     }
697   }
698 }
699 
700 #ifdef ASSERT
701  class SignatureVerifier : public StackObj {
702   public:
703     static bool is_valid_method_signature(const Symbol* sig);
704     static bool is_valid_type_signature(const Symbol* sig);
705   private:
706     static ssize_t is_valid_type(const char*, ssize_t);
707 };
708 #endif
709 #endif // SHARE_RUNTIME_SIGNATURE_HPP
< prev index next >