1 /*
   2  * Copyright (c) 1997, 2018, 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" ValueClassName ";" | "[" 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 int SignatureIterator::parse_type() {
  58   // Note: This function could be simplified by using "return T_XXX_size;"
  59   //       instead of the assignment and the break statements. However, it
  60   //       seems that the product build for win32_i486 with MS VC++ 6.0 doesn't
  61   //       work (stack underflow for some tests) - this seems to be a VC++ 6.0
  62   //       compiler bug (was problem - gri 4/27/2000).
  63   int size = -1;
  64   switch(_signature->byte_at(_index)) {
  65     case 'B': do_byte  (); if (_parameter_index < 0 ) _return_type = T_BYTE;
  66               _index++; size = T_BYTE_size   ; break;
  67     case 'C': do_char  (); if (_parameter_index < 0 ) _return_type = T_CHAR;
  68               _index++; size = T_CHAR_size   ; break;
  69     case 'D': do_double(); if (_parameter_index < 0 ) _return_type = T_DOUBLE;
  70               _index++; size = T_DOUBLE_size ; break;
  71     case 'F': do_float (); if (_parameter_index < 0 ) _return_type = T_FLOAT;
  72               _index++; size = T_FLOAT_size  ; break;
  73     case 'I': do_int   (); if (_parameter_index < 0 ) _return_type = T_INT;
  74               _index++; size = T_INT_size    ; break;
  75     case 'J': do_long  (); if (_parameter_index < 0 ) _return_type = T_LONG;
  76               _index++; size = T_LONG_size   ; break;
  77     case 'S': do_short (); if (_parameter_index < 0 ) _return_type = T_SHORT;
  78               _index++; size = T_SHORT_size  ; break;
  79     case 'Z': do_bool  (); if (_parameter_index < 0 ) _return_type = T_BOOLEAN;
  80               _index++; size = T_BOOLEAN_size; break;
  81     case 'V': do_void  (); if (_parameter_index < 0 ) _return_type = T_VOID;
  82               _index++; size = T_VOID_size;  ; break;
  83     case 'L':
  84       { int begin = ++_index;
  85         Symbol* sig = _signature;
  86         while (sig->byte_at(_index++) != ';') ;
  87         do_object(begin, _index);
  88       }
  89       if (_parameter_index < 0 ) _return_type = T_OBJECT;
  90       size = T_OBJECT_size;
  91       break;
  92     case 'Q':
  93       { int begin = ++_index;
  94       Symbol* sig = _signature;
  95       while (sig->byte_at(_index++) != ';') ;
  96       do_valuetype(begin, _index);
  97       }
  98       if (_parameter_index < 0 ) _return_type = T_VALUETYPE;
  99       size = T_VALUETYPE_size;
 100       break;
 101     case '[':
 102       { int begin = ++_index;
 103         Symbol* sig = _signature;
 104         while (sig->byte_at(_index) == '[') {
 105           _index++;
 106         }
 107         if (sig->byte_at(_index) == 'L' || sig->byte_at(_index) == 'Q') {
 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 " UINT64_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   // Need to skip over each type in the signature's argument list until a
 227   // closing ')' is found., then get the return type.  We cannot just scan
 228   // for the first ')' because ')' is a legal character in a type name.
 229   while (sig->byte_at(_index) != ')') {
 230     switch(sig->byte_at(_index)) {
 231       case 'B':
 232       case 'C':
 233       case 'D':
 234       case 'F':
 235       case 'I':
 236       case 'J':
 237       case 'S':
 238       case 'Z':
 239       case 'V':
 240         {
 241           _index++;
 242         }
 243         break;
 244       case 'Q':
 245       case 'L':
 246         {
 247           while (sig->byte_at(_index++) != ';') ;
 248         }
 249         break;
 250       case '[':
 251         {
 252           int begin = ++_index;
 253           while (sig->byte_at(_index) == '[') {
 254             _index++;
 255           }
 256           if (sig->byte_at(_index) == 'L' || sig->byte_at(_index) == 'Q' ) {
 257             while (sig->byte_at(_index++) != ';') ;
 258           } else {
 259             _index++;
 260           }
 261         }
 262         break;
 263       default:
 264         ShouldNotReachHere();
 265         break;
 266     }
 267   }
 268   expect(')');
 269   // Parse return type
 270   _parameter_index = -1;
 271   parse_type();
 272   check_signature_end();
 273   _parameter_index = 0;
 274 }
 275 
 276 
 277 void SignatureIterator::iterate() {
 278   // Parse parameters
 279   _parameter_index = 0;
 280   _index = 0;
 281   expect('(');
 282   while (_signature->byte_at(_index) != ')') _parameter_index += parse_type();
 283   expect(')');
 284   // Parse return type
 285   _parameter_index = -1;
 286   parse_type();
 287   check_signature_end();
 288   _parameter_index = 0;
 289 }
 290 
 291 
 292 // Implementation of SignatureStream
 293 SignatureStream::SignatureStream(Symbol* signature, bool is_method) :
 294                    _signature(signature), _at_return_type(false) {
 295   _begin = _end = (is_method ? 1 : 0);  // skip first '(' in method signatures
 296   _names = new GrowableArray<Symbol*>(10);
 297   next();
 298 }
 299 
 300 SignatureStream::~SignatureStream() {
 301   // decrement refcount for names created during signature parsing
 302   for (int i = 0; i < _names->length(); i++) {
 303     _names->at(i)->decrement_refcount();
 304   }
 305 }
 306 
 307 bool SignatureStream::is_done() const {
 308   return _end > _signature->utf8_length();
 309 }
 310 
 311 
 312 void SignatureStream::next_non_primitive(int t) {
 313   switch (t) {
 314     case 'L': {
 315       _type = T_OBJECT;
 316       Symbol* sig = _signature;
 317       while (sig->byte_at(_end++) != ';');
 318       break;
 319     }
 320     case 'Q': {
 321       _type = T_VALUETYPE;
 322       Symbol* sig = _signature;
 323       while (sig->byte_at(_end++) != ';');
 324       break;
 325     }
 326     case '[': {
 327       _type = T_ARRAY;
 328       Symbol* sig = _signature;
 329       char c = sig->byte_at(_end);
 330       while ('0' <= c && c <= '9') c = sig->byte_at(_end++);
 331       while (sig->byte_at(_end) == '[') {
 332         _end++;
 333         c = sig->byte_at(_end);
 334         while ('0' <= c && c <= '9') c = sig->byte_at(_end++);
 335       }
 336       switch(sig->byte_at(_end)) {
 337         case 'B':
 338         case 'C':
 339         case 'D':
 340         case 'F':
 341         case 'I':
 342         case 'J':
 343         case 'S':
 344         case 'Z':_end++; break;
 345         default: {
 346           while (sig->byte_at(_end++) != ';');
 347           break;
 348         }
 349       }
 350       break;
 351     }
 352     case ')': _end++; next(); _at_return_type = true; break;
 353     default : ShouldNotReachHere();
 354   }
 355 }
 356 
 357 
 358 bool SignatureStream::is_object() const {
 359   return _type == T_OBJECT
 360       || _type == T_ARRAY
 361       || _type == T_VALUETYPE;
 362 }
 363 
 364 bool SignatureStream::is_array() const {
 365   return _type == T_ARRAY;
 366 }
 367 
 368 Symbol* SignatureStream::as_symbol(TRAPS) {
 369   // Create a symbol from for string _begin _end
 370   int begin = _begin;
 371   int end   = _end;
 372 
 373   if (_type == T_OBJECT || _type == T_VALUETYPE) {
 374     begin++;
 375     end--;
 376     if (begin == end) {
 377       return vmSymbols::java_lang_Object();
 378     }
 379   }
 380 
 381   // Save names for cleaning up reference count at the end of
 382   // SignatureStream scope.
 383   Symbol* name = SymbolTable::new_symbol(_signature, begin, end, CHECK_NULL);
 384   _names->push(name);  // save new symbol for decrementing later
 385   return name;
 386 }
 387 
 388 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
 389                                    FailureMode failure_mode, TRAPS) {
 390   if (!is_object())  return NULL;
 391   Symbol* name = as_symbol(CHECK_NULL);
 392   if (failure_mode == ReturnNull) {
 393     return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD);
 394   } else {
 395     bool throw_error = (failure_mode == NCDFError);
 396     return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD);
 397   }
 398 }
 399 
 400 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
 401                                     FailureMode failure_mode, TRAPS) {
 402   if (!is_object())
 403     return Universe::java_mirror(type());
 404   Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
 405   if (klass == NULL)  return NULL;
 406   return klass->java_mirror();
 407 }
 408 
 409 Symbol* SignatureStream::as_symbol_or_null() {
 410   // Create a symbol from for string _begin _end
 411   ResourceMark rm;
 412 
 413   int begin = _begin;
 414   int end   = _end;
 415 
 416   if (_type == T_OBJECT || _type == T_VALUETYPE) {
 417     begin++;
 418     end--;
 419     if (begin == end) {
 420       return vmSymbols::java_lang_Object();
 421     }
 422   }
 423 
 424   char* buffer = NEW_RESOURCE_ARRAY(char, end - begin);
 425   for (int index = begin; index < end; index++) {
 426     buffer[index - begin] = _signature->byte_at(index);
 427   }
 428   Symbol* result = SymbolTable::probe(buffer, end - begin);
 429   return result;
 430 }
 431 
 432 int SignatureStream::reference_parameter_count() {
 433   int args_count = 0;
 434   for ( ; !at_return_type(); next()) {
 435     if (is_object()) {
 436       args_count++;
 437     }
 438   }
 439   return args_count;
 440 }
 441 
 442 bool SignatureVerifier::is_valid_signature(Symbol* sig) {
 443   const char* signature = (const char*)sig->bytes();
 444   ssize_t len = sig->utf8_length();
 445   if (signature == NULL || signature[0] == '\0' || len < 1) {
 446     return false;
 447   } else if (signature[0] == '(') {
 448     return is_valid_method_signature(sig);
 449   } else {
 450     return is_valid_type_signature(sig);
 451   }
 452 }
 453 
 454 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
 455   const char* method_sig = (const char*)sig->bytes();
 456   ssize_t len = sig->utf8_length();
 457   ssize_t index = 0;
 458   if (method_sig != NULL && len > 1 && method_sig[index] == '(') {
 459     ++index;
 460     while (index < len && method_sig[index] != ')') {
 461       ssize_t res = is_valid_type(&method_sig[index], len - index);
 462       if (res == -1) {
 463         return false;
 464       } else {
 465         index += res;
 466       }
 467     }
 468     if (index < len && method_sig[index] == ')') {
 469       // check the return type
 470       ++index;
 471       return (is_valid_type(&method_sig[index], len - index) == (len - index));
 472     }
 473   }
 474   return false;
 475 }
 476 
 477 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
 478   const char* type_sig = (const char*)sig->bytes();
 479   ssize_t len = sig->utf8_length();
 480   return (type_sig != NULL && len >= 1 &&
 481           (is_valid_type(type_sig, len) == len));
 482 }
 483 
 484 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
 485 // Returns -1 if it is not, or the index of the next character that is not part
 486 // of the type.  The type encoding may end before 'limit' and that's ok.
 487 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
 488   ssize_t index = 0;
 489 
 490   // Iterate over any number of array dimensions
 491   while (index < limit && type[index] == '[') ++index;
 492   if (index >= limit) {
 493     return -1;
 494   }
 495   switch (type[index]) {
 496     case 'B': case 'C': case 'D': case 'F': case 'I':
 497     case 'J': case 'S': case 'Z': case 'V':
 498       return index + 1;
 499     case 'Q': // fall through
 500     case 'L':
 501       for (index = index + 1; index < limit; ++index) {
 502         char c = type[index];
 503         if (c == ';') {
 504           return index + 1;
 505         }
 506         if (invalid_name_char(c)) {
 507           return -1;
 508         }
 509       }
 510       // fall through
 511     default: ; // fall through
 512   }
 513   return -1;
 514 }
 515 
 516 bool SignatureVerifier::invalid_name_char(char c) {
 517   switch (c) {
 518     case '\0': case '.': case ';': case '[':
 519       return true;
 520     default:
 521       return false;
 522   }
 523 }
 524 
 525 int SigEntry::count_fields(const GrowableArray<SigEntry>& sig_extended) {
 526   int values = 0;
 527   for (int i = 0; i < sig_extended.length(); i++) {
 528     if (sig_extended.at(i)._bt == T_VALUETYPE) {
 529       values++;
 530     }
 531   }
 532   return sig_extended.length() - 2 * values;
 533 }
 534 
 535 void SigEntry::fill_sig_bt(const GrowableArray<SigEntry>& sig_extended, BasicType* sig_bt_cc, int total_args_passed_cc, bool skip_vt) {
 536   int j = 0;
 537   for (int i = 0; i < sig_extended.length(); i++) {
 538     if (!skip_vt) {
 539       BasicType bt = sig_extended.at(i)._bt;
 540       // assert(bt != T_VALUETYPE, "value types should be passed as fields or reference");
 541       sig_bt_cc[j++] = bt;
 542     } else if (sig_extended.at(i)._bt != T_VALUETYPE &&
 543                (sig_extended.at(i)._bt != T_VOID ||
 544                 sig_extended.at(i-1)._bt == T_LONG ||
 545                 sig_extended.at(i-1)._bt == T_DOUBLE)) {
 546       sig_bt_cc[j++] = sig_extended.at(i)._bt;
 547     }
 548   }
 549   assert(j == total_args_passed_cc, "bad number of arguments");
 550 }