src/share/vm/runtime/signature.cpp

Thu, 24 May 2018 19:24:53 +0800

author
aoqi
date
Thu, 24 May 2018 19:24:53 +0800
changeset 8861
2a33b32dd03c
parent 8604
04d83ba48607
permissions
-rw-r--r--

#7046 Disable the compilation when branch offset is beyond short branch
Contributed-by: fujie, aoqi

     1 /*
     2  * Copyright (c) 1997, 2014, 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  */
    25 #include "precompiled.hpp"
    26 #include "classfile/symbolTable.hpp"
    27 #include "classfile/systemDictionary.hpp"
    28 #include "memory/oopFactory.hpp"
    29 #include "oops/instanceKlass.hpp"
    30 #include "oops/oop.inline.hpp"
    31 #include "oops/symbol.hpp"
    32 #include "oops/typeArrayKlass.hpp"
    33 #include "runtime/signature.hpp"
    35 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    37 // Implementation of SignatureIterator
    39 // Signature syntax:
    40 //
    41 // Signature  = "(" {Parameter} ")" ReturnType.
    42 // Parameter  = FieldType.
    43 // ReturnType = FieldType | "V".
    44 // FieldType  = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
    45 // ClassName  = string.
    48 SignatureIterator::SignatureIterator(Symbol* signature) {
    49   _signature       = signature;
    50   _parameter_index = 0;
    51 }
    53 void SignatureIterator::expect(char c) {
    54   if (_signature->byte_at(_index) != c) fatal(err_msg("expecting %c", c));
    55   _index++;
    56 }
    59 void SignatureIterator::skip_optional_size() {
    60   Symbol* sig = _signature;
    61   char c = sig->byte_at(_index);
    62   while ('0' <= c && c <= '9') c = sig->byte_at(++_index);
    63 }
    66 int SignatureIterator::parse_type() {
    67   // Note: This function could be simplified by using "return T_XXX_size;"
    68   //       instead of the assignment and the break statements. However, it
    69   //       seems that the product build for win32_i486 with MS VC++ 6.0 doesn't
    70   //       work (stack underflow for some tests) - this seems to be a VC++ 6.0
    71   //       compiler bug (was problem - gri 4/27/2000).
    72   int size = -1;
    73   switch(_signature->byte_at(_index)) {
    74     case 'B': do_byte  (); if (_parameter_index < 0 ) _return_type = T_BYTE;
    75               _index++; size = T_BYTE_size   ; break;
    76     case 'C': do_char  (); if (_parameter_index < 0 ) _return_type = T_CHAR;
    77               _index++; size = T_CHAR_size   ; break;
    78     case 'D': do_double(); if (_parameter_index < 0 ) _return_type = T_DOUBLE;
    79               _index++; size = T_DOUBLE_size ; break;
    80     case 'F': do_float (); if (_parameter_index < 0 ) _return_type = T_FLOAT;
    81               _index++; size = T_FLOAT_size  ; break;
    82     case 'I': do_int   (); if (_parameter_index < 0 ) _return_type = T_INT;
    83               _index++; size = T_INT_size    ; break;
    84     case 'J': do_long  (); if (_parameter_index < 0 ) _return_type = T_LONG;
    85               _index++; size = T_LONG_size   ; break;
    86     case 'S': do_short (); if (_parameter_index < 0 ) _return_type = T_SHORT;
    87               _index++; size = T_SHORT_size  ; break;
    88     case 'Z': do_bool  (); if (_parameter_index < 0 ) _return_type = T_BOOLEAN;
    89               _index++; size = T_BOOLEAN_size; break;
    90     case 'V': do_void  (); if (_parameter_index < 0 ) _return_type = T_VOID;
    91               _index++; size = T_VOID_size;  ; break;
    92     case 'L':
    93       { int begin = ++_index;
    94         Symbol* sig = _signature;
    95         while (sig->byte_at(_index++) != ';') ;
    96         do_object(begin, _index);
    97       }
    98       if (_parameter_index < 0 ) _return_type = T_OBJECT;
    99       size = T_OBJECT_size;
   100       break;
   101     case '[':
   102       { int begin = ++_index;
   103         skip_optional_size();
   104         Symbol* sig = _signature;
   105         while (sig->byte_at(_index) == '[') {
   106           _index++;
   107           skip_optional_size();
   108         }
   109         if (sig->byte_at(_index) == 'L') {
   110           while (sig->byte_at(_index++) != ';') ;
   111         } else {
   112           _index++;
   113         }
   114         do_array(begin, _index);
   115        if (_parameter_index < 0 ) _return_type = T_ARRAY;
   116       }
   117       size = T_ARRAY_size;
   118       break;
   119     default:
   120       ShouldNotReachHere();
   121       break;
   122   }
   123   assert(size >= 0, "size must be set");
   124   return size;
   125 }
   128 void SignatureIterator::check_signature_end() {
   129   if (_index < _signature->utf8_length()) {
   130     tty->print_cr("too many chars in signature");
   131     _signature->print_value_on(tty);
   132     tty->print_cr(" @ %d", _index);
   133   }
   134 }
   137 void SignatureIterator::dispatch_field() {
   138   // no '(', just one (field) type
   139   _index = 0;
   140   _parameter_index = 0;
   141   parse_type();
   142   check_signature_end();
   143 }
   146 void SignatureIterator::iterate_parameters() {
   147   // Parse parameters
   148   _index = 0;
   149   _parameter_index = 0;
   150   expect('(');
   151   while (_signature->byte_at(_index) != ')') _parameter_index += parse_type();
   152   expect(')');
   153   _parameter_index = 0;
   154 }
   156 // Optimized version of iterat_parameters when fingerprint is known
   157 void SignatureIterator::iterate_parameters( uint64_t fingerprint ) {
   158   uint64_t saved_fingerprint = fingerprint;
   160   // Check for too many arguments
   161   if ( fingerprint == UCONST64(-1) ) {
   162     SignatureIterator::iterate_parameters();
   163     return;
   164   }
   166   assert(fingerprint, "Fingerprint should not be 0");
   168   _parameter_index = 0;
   169   fingerprint = fingerprint >> (static_feature_size + result_feature_size);
   170   while ( 1 ) {
   171     switch ( fingerprint & parameter_feature_mask ) {
   172       case bool_parm:
   173         do_bool();
   174         _parameter_index += T_BOOLEAN_size;
   175         break;
   176       case byte_parm:
   177         do_byte();
   178         _parameter_index += T_BYTE_size;
   179         break;
   180       case char_parm:
   181         do_char();
   182         _parameter_index += T_CHAR_size;
   183         break;
   184       case short_parm:
   185         do_short();
   186         _parameter_index += T_SHORT_size;
   187         break;
   188       case int_parm:
   189         do_int();
   190         _parameter_index += T_INT_size;
   191         break;
   192       case obj_parm:
   193         do_object(0, 0);
   194         _parameter_index += T_OBJECT_size;
   195         break;
   196       case long_parm:
   197         do_long();
   198         _parameter_index += T_LONG_size;
   199         break;
   200       case float_parm:
   201         do_float();
   202         _parameter_index += T_FLOAT_size;
   203         break;
   204       case double_parm:
   205         do_double();
   206         _parameter_index += T_DOUBLE_size;
   207         break;
   208       case done_parm:
   209         return;
   210         break;
   211       default:
   212         tty->print_cr("*** parameter is %d", fingerprint & parameter_feature_mask);
   213         tty->print_cr("*** fingerprint is " PTR64_FORMAT, saved_fingerprint);
   214         ShouldNotReachHere();
   215         break;
   216     }
   217     fingerprint >>= parameter_feature_size;
   218   }
   219   _parameter_index = 0;
   220 }
   223 void SignatureIterator::iterate_returntype() {
   224   // Ignore parameters
   225   _index = 0;
   226   expect('(');
   227   Symbol* sig = _signature;
   228   // Need to skip over each type in the signature's argument list until a
   229   // closing ')' is found., then get the return type.  We cannot just scan
   230   // for the first ')' because ')' is a legal character in a type name.
   231   while (sig->byte_at(_index) != ')') {
   232     switch(sig->byte_at(_index)) {
   233       case 'B':
   234       case 'C':
   235       case 'D':
   236       case 'F':
   237       case 'I':
   238       case 'J':
   239       case 'S':
   240       case 'Z':
   241       case 'V':
   242         {
   243           _index++;
   244         }
   245         break;
   246       case 'L':
   247         {
   248           while (sig->byte_at(_index++) != ';') ;
   249         }
   250         break;
   251       case '[':
   252         {
   253           int begin = ++_index;
   254           skip_optional_size();
   255           while (sig->byte_at(_index) == '[') {
   256             _index++;
   257             skip_optional_size();
   258           }
   259           if (sig->byte_at(_index) == 'L') {
   260             while (sig->byte_at(_index++) != ';') ;
   261           } else {
   262             _index++;
   263           }
   264         }
   265         break;
   266       default:
   267         ShouldNotReachHere();
   268         break;
   269     }
   270   }
   271   expect(')');
   272   // Parse return type
   273   _parameter_index = -1;
   274   parse_type();
   275   check_signature_end();
   276   _parameter_index = 0;
   277 }
   280 void SignatureIterator::iterate() {
   281   // Parse parameters
   282   _parameter_index = 0;
   283   _index = 0;
   284   expect('(');
   285   while (_signature->byte_at(_index) != ')') _parameter_index += parse_type();
   286   expect(')');
   287   // Parse return type
   288   _parameter_index = -1;
   289   parse_type();
   290   check_signature_end();
   291   _parameter_index = 0;
   292 }
   295 // Implementation of SignatureStream
   296 SignatureStream::SignatureStream(Symbol* signature, bool is_method) :
   297                    _signature(signature), _at_return_type(false) {
   298   _begin = _end = (is_method ? 1 : 0);  // skip first '(' in method signatures
   299   _names = new GrowableArray<Symbol*>(10);
   300   next();
   301 }
   303 SignatureStream::~SignatureStream() {
   304   // decrement refcount for names created during signature parsing
   305   for (int i = 0; i < _names->length(); i++) {
   306     _names->at(i)->decrement_refcount();
   307   }
   308 }
   310 bool SignatureStream::is_done() const {
   311   return _end > _signature->utf8_length();
   312 }
   315 void SignatureStream::next_non_primitive(int t) {
   316   switch (t) {
   317     case 'L': {
   318       _type = T_OBJECT;
   319       Symbol* sig = _signature;
   320       while (sig->byte_at(_end++) != ';');
   321       break;
   322     }
   323     case '[': {
   324       _type = T_ARRAY;
   325       Symbol* sig = _signature;
   326       char c = sig->byte_at(_end);
   327       while ('0' <= c && c <= '9') c = sig->byte_at(_end++);
   328       while (sig->byte_at(_end) == '[') {
   329         _end++;
   330         c = sig->byte_at(_end);
   331         while ('0' <= c && c <= '9') c = sig->byte_at(_end++);
   332       }
   333       switch(sig->byte_at(_end)) {
   334         case 'B':
   335         case 'C':
   336         case 'D':
   337         case 'F':
   338         case 'I':
   339         case 'J':
   340         case 'S':
   341         case 'Z':_end++; break;
   342         default: {
   343           while (sig->byte_at(_end++) != ';');
   344           break;
   345         }
   346       }
   347       break;
   348     }
   349     case ')': _end++; next(); _at_return_type = true; break;
   350     default : ShouldNotReachHere();
   351   }
   352 }
   355 bool SignatureStream::is_object() const {
   356   return _type == T_OBJECT
   357       || _type == T_ARRAY;
   358 }
   360 bool SignatureStream::is_array() const {
   361   return _type == T_ARRAY;
   362 }
   364 Symbol* SignatureStream::as_symbol(TRAPS) {
   365   // Create a symbol from for string _begin _end
   366   int begin = _begin;
   367   int end   = _end;
   369   if (   _signature->byte_at(_begin) == 'L'
   370       && _signature->byte_at(_end-1) == ';') {
   371     begin++;
   372     end--;
   373   }
   375   // Save names for cleaning up reference count at the end of
   376   // SignatureStream scope.
   377   Symbol* name = SymbolTable::new_symbol(_signature, begin, end, CHECK_NULL);
   378   _names->push(name);  // save new symbol for decrementing later
   379   return name;
   380 }
   382 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
   383                                    FailureMode failure_mode, TRAPS) {
   384   if (!is_object())  return NULL;
   385   Symbol* name = as_symbol(CHECK_NULL);
   386   if (failure_mode == ReturnNull) {
   387     return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD);
   388   } else {
   389     bool throw_error = (failure_mode == NCDFError);
   390     return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD);
   391   }
   392 }
   394 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
   395                                     FailureMode failure_mode, TRAPS) {
   396   if (!is_object())
   397     return Universe::java_mirror(type());
   398   Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
   399   if (klass == NULL)  return NULL;
   400   return klass->java_mirror();
   401 }
   403 Symbol* SignatureStream::as_symbol_or_null() {
   404   // Create a symbol from for string _begin _end
   405   ResourceMark rm;
   407   int begin = _begin;
   408   int end   = _end;
   410   if (   _signature->byte_at(_begin) == 'L'
   411       && _signature->byte_at(_end-1) == ';') {
   412     begin++;
   413     end--;
   414   }
   416   char* buffer = NEW_RESOURCE_ARRAY(char, end - begin);
   417   for (int index = begin; index < end; index++) {
   418     buffer[index - begin] = _signature->byte_at(index);
   419   }
   420   Symbol* result = SymbolTable::probe(buffer, end - begin);
   421   return result;
   422 }
   424 int SignatureStream::reference_parameter_count() {
   425   int args_count = 0;
   426   for ( ; !at_return_type(); next()) {
   427     if (is_object()) {
   428       args_count++;
   429     }
   430   }
   431   return args_count;
   432 }
   434 bool SignatureVerifier::is_valid_signature(Symbol* sig) {
   435   const char* signature = (const char*)sig->bytes();
   436   ssize_t len = sig->utf8_length();
   437   if (signature == NULL || signature[0] == '\0' || len < 1) {
   438     return false;
   439   } else if (signature[0] == '(') {
   440     return is_valid_method_signature(sig);
   441   } else {
   442     return is_valid_type_signature(sig);
   443   }
   444 }
   446 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
   447   const char* method_sig = (const char*)sig->bytes();
   448   ssize_t len = sig->utf8_length();
   449   ssize_t index = 0;
   450   if (method_sig != NULL && len > 1 && method_sig[index] == '(') {
   451     ++index;
   452     while (index < len && method_sig[index] != ')') {
   453       ssize_t res = is_valid_type(&method_sig[index], len - index);
   454       if (res == -1) {
   455         return false;
   456       } else {
   457         index += res;
   458       }
   459     }
   460     if (index < len && method_sig[index] == ')') {
   461       // check the return type
   462       ++index;
   463       return (is_valid_type(&method_sig[index], len - index) == (len - index));
   464     }
   465   }
   466   return false;
   467 }
   469 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
   470   const char* type_sig = (const char*)sig->bytes();
   471   ssize_t len = sig->utf8_length();
   472   return (type_sig != NULL && len >= 1 &&
   473           (is_valid_type(type_sig, len) == len));
   474 }
   476 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
   477 // Returns -1 if it is not, or the index of the next character that is not part
   478 // of the type.  The type encoding may end before 'limit' and that's ok.
   479 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
   480   ssize_t index = 0;
   482   // Iterate over any number of array dimensions
   483   while (index < limit && type[index] == '[') ++index;
   484   if (index >= limit) {
   485     return -1;
   486   }
   487   switch (type[index]) {
   488     case 'B': case 'C': case 'D': case 'F': case 'I':
   489     case 'J': case 'S': case 'Z': case 'V':
   490       return index + 1;
   491     case 'L':
   492       for (index = index + 1; index < limit; ++index) {
   493         char c = type[index];
   494         if (c == ';') {
   495           return index + 1;
   496         }
   497         if (invalid_name_char(c)) {
   498           return -1;
   499         }
   500       }
   501       // fall through
   502     default: ; // fall through
   503   }
   504   return -1;
   505 }
   507 bool SignatureVerifier::invalid_name_char(char c) {
   508   switch (c) {
   509     case '\0': case '.': case ';': case '[':
   510       return true;
   511     default:
   512       return false;
   513   }
   514 }

mercurial