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