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 ";" | "Q" ValueTypeName ";" | "[" 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 'Q':
 101       { int begin = ++_index;
 102         Symbol* sig = _signature;
 103         while (sig->byte_at(_index++) != ';') ;
 104         do_valuetype(begin, _index);
 105       }
 106       if (_parameter_index < 0 ) _return_type = T_VALUETYPE;
 107       size = T_VALUETYPE_size;
 108       break;
 109     case '[':
 110       { int begin = ++_index;
 111         skip_optional_size();
 112         Symbol* sig = _signature;
 113         while (sig->byte_at(_index) == '[') {
 114           _index++;
 115           skip_optional_size();
 116         }
 117         if (sig->byte_at(_index) == 'L' || sig->byte_at(_index) == 'Q') {
 118           while (sig->byte_at(_index++) != ';') ;
 119         } else {
 120           _index++;
 121         }
 122         do_array(begin, _index);
 123        if (_parameter_index < 0 ) _return_type = T_ARRAY;
 124       }
 125       size = T_ARRAY_size;
 126       break;
 127     default:
 128       ShouldNotReachHere();
 129       break;
 130   }
 131   assert(size >= 0, "size must be set");
 132   return size;
 133 }
 134 
 135 
 136 void SignatureIterator::check_signature_end() {
 137   if (_index < _signature->utf8_length()) {
 138     tty->print_cr("too many chars in signature");
 139     _signature->print_value_on(tty);
 140     tty->print_cr(" @ %d", _index);
 141   }
 142 }
 143 
 144 
 145 void SignatureIterator::dispatch_field() {
 146   // no '(', just one (field) type
 147   _index = 0;
 148   _parameter_index = 0;
 149   parse_type();
 150   check_signature_end();
 151 }
 152 
 153 
 154 void SignatureIterator::iterate_parameters() {
 155   // Parse parameters
 156   _index = 0;
 157   _parameter_index = 0;
 158   expect('(');
 159   while (_signature->byte_at(_index) != ')') _parameter_index += parse_type();
 160   expect(')');
 161   _parameter_index = 0;
 162 }
 163 
 164 // Optimized version of iterate_parameters when fingerprint is known
 165 void SignatureIterator::iterate_parameters( uint64_t fingerprint ) {
 166   uint64_t saved_fingerprint = fingerprint;
 167 
 168   // Check for too many arguments
 169   if (fingerprint == (uint64_t)CONST64(-1)) {
 170     SignatureIterator::iterate_parameters();
 171     return;
 172   }
 173 
 174   assert(fingerprint, "Fingerprint should not be 0");
 175 
 176   _parameter_index = 0;
 177   fingerprint = fingerprint >> (static_feature_size + result_feature_size);
 178   while ( 1 ) {
 179     switch ( fingerprint & parameter_feature_mask ) {
 180       case bool_parm:
 181         do_bool();
 182         _parameter_index += T_BOOLEAN_size;
 183         break;
 184       case byte_parm:
 185         do_byte();
 186         _parameter_index += T_BYTE_size;
 187         break;
 188       case char_parm:
 189         do_char();
 190         _parameter_index += T_CHAR_size;
 191         break;
 192       case short_parm:
 193         do_short();
 194         _parameter_index += T_SHORT_size;
 195         break;
 196       case int_parm:
 197         do_int();
 198         _parameter_index += T_INT_size;
 199         break;
 200       case obj_parm:
 201         do_object(0, 0);
 202         _parameter_index += T_OBJECT_size;
 203         break;
 204       case valuetype_parm:
 205         do_valuetype(0,0);
 206         _parameter_index += T_VALUETYPE_size;
 207         break;
 208       case long_parm:
 209         do_long();
 210         _parameter_index += T_LONG_size;
 211         break;
 212       case float_parm:
 213         do_float();
 214         _parameter_index += T_FLOAT_size;
 215         break;
 216       case double_parm:
 217         do_double();
 218         _parameter_index += T_DOUBLE_size;
 219         break;
 220       case done_parm:
 221         return;
 222         break;
 223       default:
 224         tty->print_cr("*** parameter is " UINT64_FORMAT, fingerprint & parameter_feature_mask);
 225         tty->print_cr("*** fingerprint is " PTR64_FORMAT, saved_fingerprint);
 226         ShouldNotReachHere();
 227         break;
 228     }
 229     fingerprint >>= parameter_feature_size;
 230   }
 231   _parameter_index = 0;
 232 }
 233 
 234 
 235 void SignatureIterator::iterate_returntype() {
 236   // Ignore parameters
 237   _index = 0;
 238   expect('(');
 239   Symbol* sig = _signature;
 240   // Need to skip over each type in the signature's argument list until a
 241   // closing ')' is found., then get the return type.  We cannot just scan
 242   // for the first ')' because ')' is a legal character in a type name.
 243   while (sig->byte_at(_index) != ')') {
 244     switch(sig->byte_at(_index)) {
 245       case 'B':
 246       case 'C':
 247       case 'D':
 248       case 'F':
 249       case 'I':
 250       case 'J':
 251       case 'S':
 252       case 'Z':
 253       case 'V':
 254         {
 255           _index++;
 256         }
 257         break;
 258       case 'Q':
 259       case 'L':
 260         {
 261           while (sig->byte_at(_index++) != ';') ;
 262         }
 263         break;
 264       case '[':
 265         {
 266           int begin = ++_index;
 267           skip_optional_size();
 268           while (sig->byte_at(_index) == '[') {
 269             _index++;
 270             skip_optional_size();
 271           }
 272           if (sig->byte_at(_index) == 'L' || sig->byte_at(_index) == 'Q') {
 273             while (sig->byte_at(_index++) != ';') ;
 274           } else {
 275             _index++;
 276           }
 277         }
 278         break;
 279       default:
 280         ShouldNotReachHere();
 281         break;
 282     }
 283   }
 284   expect(')');
 285   // Parse return type
 286   _parameter_index = -1;
 287   parse_type();
 288   check_signature_end();
 289   _parameter_index = 0;
 290 }
 291 
 292 
 293 void SignatureIterator::iterate() {
 294   // Parse parameters
 295   _parameter_index = 0;
 296   _index = 0;
 297   expect('(');
 298   while (_signature->byte_at(_index) != ')') _parameter_index += parse_type();
 299   expect(')');
 300   // Parse return type
 301   _parameter_index = -1;
 302   parse_type();
 303   check_signature_end();
 304   _parameter_index = 0;
 305 }
 306 
 307 
 308 // Implementation of SignatureStream
 309 SignatureStream::SignatureStream(Symbol* signature, bool is_method) :
 310                    _signature(signature), _at_return_type(false) {
 311   _begin = _end = (is_method ? 1 : 0);  // skip first '(' in method signatures
 312   _names = new GrowableArray<Symbol*>(10);
 313   next();
 314 }
 315 
 316 SignatureStream::~SignatureStream() {
 317   // decrement refcount for names created during signature parsing
 318   for (int i = 0; i < _names->length(); i++) {
 319     _names->at(i)->decrement_refcount();
 320   }
 321 }
 322 
 323 bool SignatureStream::is_done() const {
 324   return _end > _signature->utf8_length();
 325 }
 326 
 327 
 328 void SignatureStream::next_non_primitive(int t) {
 329   switch (t) {
 330     case 'L': {
 331       _type = T_OBJECT;
 332       Symbol* sig = _signature;
 333       while (sig->byte_at(_end++) != ';');
 334       break;
 335     }
 336     case 'Q': {
 337       _type = T_VALUETYPE;
 338       Symbol* sig = _signature;
 339       while (sig->byte_at(_end++) != ';');
 340       break;
 341     }
 342     case '[': {
 343       _type = T_ARRAY;
 344       Symbol* sig = _signature;
 345       char c = sig->byte_at(_end);
 346       while ('0' <= c && c <= '9') c = sig->byte_at(_end++);
 347       while (sig->byte_at(_end) == '[') {
 348         _end++;
 349         c = sig->byte_at(_end);
 350         while ('0' <= c && c <= '9') c = sig->byte_at(_end++);
 351       }
 352       switch(sig->byte_at(_end)) {
 353         case 'B':
 354         case 'C':
 355         case 'D':
 356         case 'F':
 357         case 'I':
 358         case 'J':
 359         case 'S':
 360         case 'Z':_end++; break;
 361         default: {
 362           while (sig->byte_at(_end++) != ';');
 363           break;
 364         }
 365       }
 366       break;
 367     }
 368     case ')': _end++; next(); _at_return_type = true; break;
 369     default : ShouldNotReachHere();
 370   }
 371 }
 372 
 373 
 374 bool SignatureStream::is_object() const {
 375   return _type == T_OBJECT
 376       || _type == T_ARRAY
 377       || _type == T_VALUETYPE;
 378 }
 379 
 380 bool SignatureStream::is_array() const {
 381   return _type == T_ARRAY;
 382 }
 383 
 384 Symbol* SignatureStream::as_symbol(TRAPS) {
 385   // Create a symbol from for string _begin _end
 386   int begin = _begin;
 387   int end   = _end;
 388 
 389   if (_type == T_OBJECT || _type == T_VALUETYPE) {
 390     begin++;
 391     end--;
 392     if (begin == end) {
 393       return (_type == T_OBJECT) ? vmSymbols::java_lang_Object() : vmSymbols::java_lang____Value();
 394     }
 395   }
 396 
 397   // Save names for cleaning up reference count at the end of
 398   // SignatureStream scope.
 399   Symbol* name = SymbolTable::new_symbol(_signature, begin, end, CHECK_NULL);
 400   _names->push(name);  // save new symbol for decrementing later
 401   return name;
 402 }
 403 
 404 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
 405                                    FailureMode failure_mode, TRAPS) {
 406   if (!is_object())  return NULL;
 407   Symbol* name = as_symbol(CHECK_NULL);
 408   if (failure_mode == ReturnNull) {
 409     return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD);
 410   } else {
 411     bool throw_error = (failure_mode == NCDFError);
 412     return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD);
 413   }
 414 }
 415 
 416 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
 417                                     FailureMode failure_mode, TRAPS) {
 418   if (!is_object())
 419     return Universe::java_mirror(type());
 420   Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
 421   if (klass == NULL)  return NULL;
 422   return klass->java_mirror();
 423 }
 424 
 425 Symbol* SignatureStream::as_symbol_or_null() {
 426   // Create a symbol from for string _begin _end
 427   ResourceMark rm;
 428 
 429   int begin = _begin;
 430   int end   = _end;
 431 
 432   if (_type == T_OBJECT || _type == T_VALUETYPE) {
 433     begin++;
 434     end--;
 435     if (begin == end) {
 436       return (_type == T_OBJECT) ? vmSymbols::java_lang_Object() : vmSymbols::java_lang____Value();
 437     }
 438   }
 439 
 440   char* buffer = NEW_RESOURCE_ARRAY(char, end - begin);
 441   for (int index = begin; index < end; index++) {
 442     buffer[index - begin] = _signature->byte_at(index);
 443   }
 444   Symbol* result = SymbolTable::probe(buffer, end - begin);
 445   return result;
 446 }
 447 
 448 int SignatureStream::reference_parameter_count() {
 449   int args_count = 0;
 450   for ( ; !at_return_type(); next()) {
 451     if (is_object()) {
 452       args_count++;
 453     }
 454   }
 455   return args_count;
 456 }
 457 
 458 bool SignatureVerifier::is_valid_signature(Symbol* sig) {
 459   const char* signature = (const char*)sig->bytes();
 460   ssize_t len = sig->utf8_length();
 461   if (signature == NULL || signature[0] == '\0' || len < 1) {
 462     return false;
 463   } else if (signature[0] == '(') {
 464     return is_valid_method_signature(sig);
 465   } else {
 466     return is_valid_type_signature(sig);
 467   }
 468 }
 469 
 470 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
 471   const char* method_sig = (const char*)sig->bytes();
 472   ssize_t len = sig->utf8_length();
 473   ssize_t index = 0;
 474   if (method_sig != NULL && len > 1 && method_sig[index] == '(') {
 475     ++index;
 476     while (index < len && method_sig[index] != ')') {
 477       ssize_t res = is_valid_type(&method_sig[index], len - index);
 478       if (res == -1) {
 479         return false;
 480       } else {
 481         index += res;
 482       }
 483     }
 484     if (index < len && method_sig[index] == ')') {
 485       // check the return type
 486       ++index;
 487       return (is_valid_type(&method_sig[index], len - index) == (len - index));
 488     }
 489   }
 490   return false;
 491 }
 492 
 493 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
 494   const char* type_sig = (const char*)sig->bytes();
 495   ssize_t len = sig->utf8_length();
 496   return (type_sig != NULL && len >= 1 &&
 497           (is_valid_type(type_sig, len) == len));
 498 }
 499 
 500 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
 501 // Returns -1 if it is not, or the index of the next character that is not part
 502 // of the type.  The type encoding may end before 'limit' and that's ok.
 503 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
 504   ssize_t index = 0;
 505 
 506   // Iterate over any number of array dimensions
 507   while (index < limit && type[index] == '[') ++index;
 508   if (index >= limit) {
 509     return -1;
 510   }
 511   switch (type[index]) {
 512     case 'B': case 'C': case 'D': case 'F': case 'I':
 513     case 'J': case 'S': case 'Z': case 'V':
 514       return index + 1;
 515     case 'Q':
 516     case 'L':
 517       for (index = index + 1; index < limit; ++index) {
 518         char c = type[index];
 519         if (c == ';') {
 520           return index + 1;
 521         }
 522         if (invalid_name_char(c)) {
 523           return -1;
 524         }
 525       }
 526       // fall through
 527     default: ; // fall through
 528   }
 529   return -1;
 530 }
 531 
 532 bool SignatureVerifier::invalid_name_char(char c) {
 533   switch (c) {
 534     case '\0': case '.': case ';': case '[':
 535       return true;
 536     default:
 537       return false;
 538   }
 539 }
 540 
 541 int SigEntry::count_fields(const GrowableArray<SigEntry>& sig_extended) {
 542   int values = 0;
 543   for (int i = 0; i < sig_extended.length(); i++) {
 544     if (sig_extended.at(i)._bt == T_VALUETYPE) {
 545       values++;
 546     }
 547   }
 548   return sig_extended.length() - 2 * values;
 549 }
 550 
 551 void SigEntry::fill_sig_bt(const GrowableArray<SigEntry>& sig_extended, BasicType* sig_bt_cc, int total_args_passed_cc, bool skip_vt) {
 552   int j = 0;
 553   for (int i = 0; i < sig_extended.length(); i++) {
 554     if (!skip_vt) {
 555       BasicType bt = sig_extended.at(i)._bt;
 556       assert(bt != T_VALUETYPE, "value types should be passed as fields or reference");
 557       sig_bt_cc[j++] = bt;
 558     } else if (sig_extended.at(i)._bt != T_VALUETYPE &&
 559                (sig_extended.at(i)._bt != T_VOID ||
 560                 sig_extended.at(i-1)._bt == T_LONG ||
 561                 sig_extended.at(i-1)._bt == T_DOUBLE)) {
 562       sig_bt_cc[j++] = sig_extended.at(i)._bt;
 563     }
 564   }
 565   assert(j == total_args_passed_cc, "bad number of arguments");
 566 }