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