1 /* 2 * Copyright (c) 1997, 2024, 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 "asm/assembler.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "memory/oopFactory.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "memory/universe.hpp" 32 #include "oops/instanceKlass.hpp" 33 #include "oops/klass.inline.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "oops/symbol.hpp" 36 #include "oops/typeArrayKlass.hpp" 37 #include "oops/inlineKlass.inline.hpp" 38 #include "runtime/fieldDescriptor.inline.hpp" 39 #include "runtime/handles.inline.hpp" 40 #include "runtime/interfaceSupport.inline.hpp" 41 #include "runtime/safepointVerifiers.hpp" 42 #include "runtime/sharedRuntime.hpp" 43 #include "runtime/signature.hpp" 44 #include "runtime/sharedRuntime.hpp" 45 46 // Implementation of SignatureIterator 47 48 // Signature syntax: 49 // 50 // Signature = "(" {Parameter} ")" ReturnType. 51 // Parameter = FieldType. 52 // ReturnType = FieldType | "V". 53 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "Q" ValueClassName ";" | "[" FieldType. 54 // ClassName = string. 55 56 // The ClassName string can be any JVM-style UTF8 string except: 57 // - an empty string (the empty string is never a name of any kind) 58 // - a string which begins or ends with slash '/' (the package separator) 59 // - a string which contains adjacent slashes '//' (no empty package names) 60 // - a string which contains a semicolon ';' (the end-delimiter) 61 // - a string which contains a left bracket '[' (the array marker) 62 // - a string which contains a dot '.' (the external package separator) 63 // 64 // Other "meta-looking" characters, such as '(' and '<' and '+', 65 // are perfectly legitimate within a class name, for the JVM. 66 // Class names which contain double slashes ('a//b') and non-initial 67 // brackets ('a[b]') are reserved for possible enrichment of the 68 // type language. 69 70 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) { 71 if (!fp_is_valid(fingerprint)) { 72 _fingerprint = fingerprint; 73 _return_type = T_ILLEGAL; 74 } else if (fingerprint != _fingerprint) { 75 assert(_fingerprint == zero_fingerprint(), "consistent fingerprint values"); 76 _fingerprint = fingerprint; 77 _return_type = fp_return_type(fingerprint); 78 } 79 } 80 81 BasicType SignatureIterator::return_type() { 82 if (_return_type == T_ILLEGAL) { 83 SignatureStream ss(_signature); 84 ss.skip_to_return_type(); 85 _return_type = ss.type(); 86 assert(_return_type != T_ILLEGAL, "illegal return type"); 87 } 88 return _return_type; 89 } 90 91 bool SignatureIterator::fp_is_valid_type(BasicType type, bool for_return_type) { 92 assert(type != (BasicType)fp_parameters_done, "fingerprint is incorrectly at done"); 93 assert(((int)type & ~fp_parameter_feature_mask) == 0, "fingerprint feature mask yielded non-zero value"); 94 return (is_java_primitive(type) || 95 is_reference_type(type) || 96 (for_return_type && type == T_VOID)); 97 } 98 99 ArgumentSizeComputer::ArgumentSizeComputer(Symbol* signature) 100 : SignatureIterator(signature) 101 { 102 _size = 0; 103 do_parameters_on(this); // non-virtual template execution 104 } 105 106 ArgumentCount::ArgumentCount(Symbol* signature) 107 : SignatureIterator(signature) 108 { 109 _size = 0; 110 do_parameters_on(this); // non-virtual template execution 111 } 112 113 ReferenceArgumentCount::ReferenceArgumentCount(Symbol* signature) 114 : SignatureIterator(signature) 115 { 116 _refs = 0; 117 do_parameters_on(this); // non-virtual template execution 118 } 119 120 #if !defined(_LP64) || defined(ZERO) || defined(ASSERT) 121 static int compute_num_stack_arg_slots(Symbol* signature, int sizeargs, bool is_static) { 122 ResourceMark rm; 123 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); 124 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); 125 126 int sig_index = 0; 127 if (!is_static) { 128 sig_bt[sig_index++] = T_OBJECT; // 'this' 129 } 130 for (SignatureStream ss(signature); !ss.at_return_type(); ss.next()) { 131 BasicType t = ss.type(); 132 assert(type2size[t] == 1 || type2size[t] == 2, "size is 1 or 2"); 133 sig_bt[sig_index++] = t; 134 if (type2size[t] == 2) { 135 sig_bt[sig_index++] = T_VOID; 136 } 137 } 138 assert(sig_index == sizeargs, "sig_index: %d sizeargs: %d", sig_index, sizeargs); 139 140 return SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs); 141 } 142 #endif 143 144 void Fingerprinter::compute_fingerprint_and_return_type(bool static_flag) { 145 // See if we fingerprinted this method already 146 if (_method != nullptr) { 147 assert(!static_flag, "must not be passed by caller"); 148 static_flag = _method->is_static(); 149 _fingerprint = _method->constMethod()->fingerprint(); 150 151 if (_fingerprint != zero_fingerprint()) { 152 _return_type = _method->result_type(); 153 assert(is_java_type(_return_type), "return type must be a java type"); 154 return; 155 } 156 157 if (_method->size_of_parameters() > fp_max_size_of_parameters) { 158 _fingerprint = overflow_fingerprint(); 159 _method->constMethod()->set_fingerprint(_fingerprint); 160 // as long as we are here compute the return type: 161 _return_type = ResultTypeFinder(_method->signature()).type(); 162 assert(is_java_type(_return_type), "return type must be a java type"); 163 return; 164 } 165 } 166 167 // Note: This will always take the slow path, since _fp==zero_fp. 168 initialize_accumulator(); 169 initialize_calling_convention(static_flag); 170 do_parameters_on(this); 171 assert(fp_is_valid_type(_return_type, true), "bad result type"); 172 173 // Fill in the return type and static bits: 174 _accumulator |= _return_type << fp_static_feature_size; 175 if (static_flag) { 176 _accumulator |= fp_is_static_bit; 177 } else { 178 _param_size += 1; // this is the convention for Method::compute_size_of_parameters 179 } 180 181 #if defined(_LP64) && !defined(ZERO) 182 #ifdef ASSERT 183 int dbg_stack_arg_slots = compute_num_stack_arg_slots(_signature, _param_size, static_flag); 184 assert(_stack_arg_slots == dbg_stack_arg_slots, "fingerprinter: %d full: %d", _stack_arg_slots, dbg_stack_arg_slots); 185 #endif 186 #else 187 // Fallback: computed _stack_arg_slots is unreliable, compute directly. 188 _stack_arg_slots = compute_num_stack_arg_slots(_signature, _param_size, static_flag); 189 #endif 190 191 // Detect overflow. (We counted _param_size correctly.) 192 if (_method == nullptr && _param_size > fp_max_size_of_parameters) { 193 // We did a one-pass computation of argument size, return type, 194 // and fingerprint. 195 _fingerprint = overflow_fingerprint(); 196 return; 197 } 198 199 assert(_shift_count < BitsPerLong, 200 "shift count overflow %d (%d vs. %d): %s", 201 _shift_count, _param_size, fp_max_size_of_parameters, 202 _signature->as_C_string()); 203 assert((_accumulator >> _shift_count) == fp_parameters_done, "must be zero"); 204 205 // This is the result, along with _return_type: 206 _fingerprint = _accumulator; 207 208 // Cache the result on the method itself: 209 if (_method != nullptr) { 210 _method->constMethod()->set_fingerprint(_fingerprint); 211 } 212 } 213 214 void Fingerprinter::initialize_calling_convention(bool static_flag) { 215 _int_args = 0; 216 _fp_args = 0; 217 218 if (!static_flag) { // `this` takes up an int register 219 _int_args++; 220 } 221 } 222 223 void Fingerprinter::do_type_calling_convention(BasicType type) { 224 // We compute the number of slots for stack-passed arguments in compiled calls. 225 // TODO: SharedRuntime::java_calling_convention is the shared code that knows all details 226 // about the platform-specific calling conventions. This method tries to compute the stack 227 // args number... poorly, at least for 32-bit ports and for zero. Current code has the fallback 228 // that recomputes the stack args number from SharedRuntime::java_calling_convention. 229 #if defined(_LP64) && !defined(ZERO) 230 switch (type) { 231 case T_VOID: 232 break; 233 case T_BOOLEAN: 234 case T_CHAR: 235 case T_BYTE: 236 case T_SHORT: 237 case T_INT: 238 if (_int_args < Argument::n_int_register_parameters_j) { 239 _int_args++; 240 } else { 241 #if defined(PPC64) || defined(S390) 242 _stack_arg_slots += 1; 243 #else 244 _stack_arg_slots = align_up(_stack_arg_slots, 2); 245 _stack_arg_slots += 1; 246 #endif // defined(PPC64) || defined(S390) 247 } 248 break; 249 case T_LONG: 250 case T_OBJECT: 251 case T_ARRAY: 252 case T_ADDRESS: 253 if (_int_args < Argument::n_int_register_parameters_j) { 254 _int_args++; 255 } else { 256 _stack_arg_slots = align_up(_stack_arg_slots, 2); 257 _stack_arg_slots += 2; 258 } 259 break; 260 case T_FLOAT: 261 if (_fp_args < Argument::n_float_register_parameters_j) { 262 _fp_args++; 263 } else { 264 #if defined(PPC64) || defined(S390) 265 _stack_arg_slots += 1; 266 #else 267 _stack_arg_slots = align_up(_stack_arg_slots, 2); 268 _stack_arg_slots += 1; 269 #endif // defined(PPC64) || defined(S390) 270 } 271 break; 272 case T_DOUBLE: 273 if (_fp_args < Argument::n_float_register_parameters_j) { 274 _fp_args++; 275 } else { 276 _stack_arg_slots = align_up(_stack_arg_slots, 2); 277 _stack_arg_slots += 2; 278 } 279 break; 280 default: 281 ShouldNotReachHere(); 282 break; 283 } 284 #endif 285 } 286 287 // Implementation of SignatureStream 288 289 static inline BasicType decode_signature_char(int ch) { 290 switch (ch) { 291 #define EACH_SIG(ch, bt, ignore) \ 292 case ch: return bt; 293 SIGNATURE_TYPES_DO(EACH_SIG, ignore) 294 #undef EACH_SIG 295 } 296 return (BasicType)0; 297 } 298 299 SignatureStream::SignatureStream(const Symbol* signature, 300 bool is_method) { 301 assert(!is_method || signature->starts_with(JVM_SIGNATURE_FUNC), 302 "method signature required"); 303 _signature = signature; 304 _limit = signature->utf8_length(); 305 int oz = (is_method ? _s_method : _s_field); 306 _state = oz; 307 _begin = _end = oz; // skip first '(' in method signatures 308 _array_prefix = 0; // just for definiteness 309 310 // assigning java/lang/Object to _previous_name means we can 311 // avoid a number of null checks in the parser 312 _previous_name = vmSymbols::java_lang_Object(); 313 _names = nullptr; 314 next(); 315 } 316 317 SignatureStream::~SignatureStream() { 318 if (_previous_name == vmSymbols::java_lang_Object()) { 319 // no names were created 320 assert(_names == nullptr, "_names unexpectedly created"); 321 return; 322 } 323 324 // decrement refcount for names created during signature parsing 325 _previous_name->decrement_refcount(); 326 if (_names != nullptr) { 327 for (int i = 0; i < _names->length(); i++) { 328 _names->at(i)->decrement_refcount(); 329 } 330 } 331 } 332 333 inline int SignatureStream::scan_type(BasicType type) { 334 const u1* base = _signature->bytes(); 335 int end = _end; 336 int limit = _limit; 337 const u1* tem; 338 switch (type) { 339 case T_OBJECT: 340 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end); 341 return (tem == nullptr ? limit : pointer_delta_as_int(tem + 1, base)); 342 343 case T_ARRAY: 344 while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; } 345 // If we discovered only the string of '[', this means something is wrong. 346 if (end >= limit) { 347 assert(false, "Invalid type detected"); 348 return limit; 349 } 350 _array_prefix = end - _end; // number of '[' chars just skipped 351 if (Signature::has_envelope(base[end])) { 352 tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end); 353 return (tem == nullptr ? limit : pointer_delta_as_int(tem + 1, base)); 354 } 355 // Skipping over a single character for a primitive type. 356 assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected"); 357 return end + 1; 358 359 default: 360 // Skipping over a single character for a primitive type (or void). 361 assert(!is_reference_type(type), "only primitives or void expected"); 362 return end + 1; 363 } 364 } 365 366 void SignatureStream::next() { 367 const Symbol* sig = _signature; 368 int len = _limit; 369 if (_end >= len) { set_done(); return; } 370 _begin = _end; 371 int ch = sig->char_at(_begin); 372 if (ch == JVM_SIGNATURE_ENDFUNC) { 373 assert(_state == _s_method, "must be in method"); 374 _state = _s_method_return; 375 _begin = ++_end; 376 if (_end >= len) { set_done(); return; } 377 ch = sig->char_at(_begin); 378 } 379 BasicType bt = decode_signature_char(ch); 380 assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch); 381 _type = bt; 382 _end = scan_type(bt); 383 } 384 385 int SignatureStream::skip_whole_array_prefix() { 386 assert(_type == T_ARRAY, "must be"); 387 388 // we are stripping all levels of T_ARRAY, 389 // so we must decode the next character 390 int whole_array_prefix = _array_prefix; 391 int new_begin = _begin + whole_array_prefix; 392 _begin = new_begin; 393 int ch = _signature->char_at(new_begin); 394 BasicType bt = decode_signature_char(ch); 395 assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch); 396 _type = bt; 397 assert(bt != T_VOID && bt != T_ARRAY, "bad signature type"); 398 // Don't bother to re-scan, since it won't change the value of _end. 399 return whole_array_prefix; 400 } 401 402 bool Signature::is_valid_array_signature(const Symbol* sig) { 403 assert(sig->utf8_length() > 1, "this should already have been checked"); 404 assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked"); 405 // The first character is already checked 406 int i = 1; 407 int len = sig->utf8_length(); 408 // First skip all '['s 409 while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++; 410 411 // Check type 412 switch(sig->char_at(i)) { 413 case JVM_SIGNATURE_BYTE: 414 case JVM_SIGNATURE_CHAR: 415 case JVM_SIGNATURE_DOUBLE: 416 case JVM_SIGNATURE_FLOAT: 417 case JVM_SIGNATURE_INT: 418 case JVM_SIGNATURE_LONG: 419 case JVM_SIGNATURE_SHORT: 420 case JVM_SIGNATURE_BOOLEAN: 421 // If it is an array, the type is the last character 422 return (i + 1 == len); 423 case JVM_SIGNATURE_CLASS: 424 // If it is an object, the last character must be a ';' 425 return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS; 426 } 427 return false; 428 } 429 430 BasicType Signature::basic_type(int ch) { 431 BasicType btcode = decode_signature_char(ch); 432 if (btcode == 0) return T_ILLEGAL; 433 return btcode; 434 } 435 436 Symbol* Signature::strip_envelope(const Symbol* signature) { 437 assert(has_envelope(signature), "precondition"); 438 return SymbolTable::new_symbol((char*) signature->bytes() + 1, 439 signature->utf8_length() - 2); 440 } 441 442 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len; 443 static const char jl_str[] = "java/lang/"; 444 445 #ifdef ASSERT 446 static bool signature_symbols_sane() { 447 static bool done; 448 if (done) return true; 449 done = true; 450 // test some tense code that looks for common symbol names: 451 assert(vmSymbols::java_lang_Object()->utf8_length() == jl_object_len && 452 vmSymbols::java_lang_Object()->starts_with(jl_str, jl_len) && 453 vmSymbols::java_lang_Object()->ends_with("Object", object_len) && 454 vmSymbols::java_lang_Object()->is_permanent() && 455 vmSymbols::java_lang_String()->utf8_length() == jl_object_len && 456 vmSymbols::java_lang_String()->starts_with(jl_str, jl_len) && 457 vmSymbols::java_lang_String()->ends_with("String", object_len) && 458 vmSymbols::java_lang_String()->is_permanent(), 459 "sanity"); 460 return true; 461 } 462 #endif //ASSERT 463 464 // returns a symbol; the caller is responsible for decrementing it 465 Symbol* SignatureStream::find_symbol() { 466 // Create a symbol from for string _begin _end 467 int begin = raw_symbol_begin(); 468 int end = raw_symbol_end(); 469 470 const char* symbol_chars = (const char*)_signature->base() + begin; 471 int len = end - begin; 472 473 // Quick check for common symbols in signatures 474 assert(signature_symbols_sane(), "incorrect signature sanity check"); 475 if (len == jl_object_len && 476 memcmp(symbol_chars, jl_str, jl_len) == 0) { 477 if (memcmp("String", symbol_chars + jl_len, object_len) == 0) { 478 return vmSymbols::java_lang_String(); 479 } else if (memcmp("Object", symbol_chars + jl_len, object_len) == 0) { 480 return vmSymbols::java_lang_Object(); 481 } 482 } 483 484 Symbol* name = _previous_name; 485 if (name->equals(symbol_chars, len)) { 486 return name; 487 } 488 489 // Save names for cleaning up reference count at the end of 490 // SignatureStream scope. 491 name = SymbolTable::new_symbol(symbol_chars, len); 492 493 // Only allocate the GrowableArray for the _names buffer if more than 494 // one name is being processed in the signature. 495 if (!_previous_name->is_permanent()) { 496 if (_names == nullptr) { 497 _names = new GrowableArray<Symbol*>(10); 498 } 499 _names->push(_previous_name); 500 } 501 _previous_name = name; 502 return name; 503 } 504 505 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) { 506 ThreadInVMfromUnknown tiv; 507 JavaThread* THREAD = JavaThread::current(); 508 HandleMark hm(THREAD); 509 Handle class_loader(THREAD, holder->class_loader()); 510 Klass* k = as_klass(class_loader, SignatureStream::CachedOrNull, THREAD); 511 assert(!HAS_PENDING_EXCEPTION, "Should never throw"); 512 if (k != nullptr && k->is_inline_klass()) { 513 return InlineKlass::cast(k); 514 } else { 515 return nullptr; 516 } 517 } 518 519 Klass* SignatureStream::as_klass(Handle class_loader, FailureMode failure_mode, TRAPS) { 520 if (!is_reference()) { 521 return nullptr; 522 } 523 Symbol* name = as_symbol(); 524 Klass* k = nullptr; 525 if (failure_mode == ReturnNull) { 526 // Note: SD::resolve_or_null returns null for most failure modes, 527 // but not all. Circularity errors, invalid PDs, etc., throw. 528 k = SystemDictionary::resolve_or_null(name, class_loader, CHECK_NULL); 529 } else if (failure_mode == CachedOrNull) { 530 NoSafepointVerifier nsv; // no loading, now, we mean it! 531 assert(!HAS_PENDING_EXCEPTION, ""); 532 k = SystemDictionary::find_instance_klass(THREAD, name, class_loader); 533 // SD::find does not trigger loading, so there should be no throws 534 // Still, bad things can happen, so we CHECK_NULL and ask callers 535 // to do likewise. 536 return k; 537 } else { 538 // The only remaining failure mode is NCDFError. 539 // The test here allows for an additional mode CNFException 540 // if callers need to request the reflective error instead. 541 bool throw_error = (failure_mode == NCDFError); 542 k = SystemDictionary::resolve_or_fail(name, class_loader, throw_error, CHECK_NULL); 543 } 544 545 return k; 546 } 547 548 oop SignatureStream::as_java_mirror(Handle class_loader, FailureMode failure_mode, TRAPS) { 549 if (!is_reference()) { 550 return Universe::java_mirror(type()); 551 } 552 Klass* klass = as_klass(class_loader, failure_mode, CHECK_NULL); 553 if (klass == nullptr) { 554 return nullptr; 555 } 556 return klass->java_mirror(); 557 } 558 559 void SignatureStream::skip_to_return_type() { 560 while (!at_return_type()) { 561 next(); 562 } 563 } 564 565 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, 566 Handle class_loader, 567 bool is_method) 568 : SignatureStream(signature, is_method), _class_loader(class_loader) 569 { 570 initialize_load_origin(nullptr); 571 } 572 573 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method) 574 : SignatureStream(signature, is_method) 575 { 576 assert(load_origin != nullptr, ""); 577 initialize_load_origin(load_origin); 578 } 579 580 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method) 581 : SignatureStream(method->signature(), true) 582 { 583 initialize_load_origin(method->method_holder()); 584 } 585 586 void ResolvingSignatureStream::cache_handles() { 587 assert(_load_origin != nullptr, ""); 588 JavaThread* current = JavaThread::current(); 589 _class_loader = Handle(current, _load_origin->class_loader()); 590 } 591 592 #ifdef ASSERT 593 extern bool signature_constants_sane(); // called from basic_types_init() 594 595 bool signature_constants_sane() { 596 // for the lookup table, test every 8-bit code point, and then some: 597 for (int i = -256; i <= 256; i++) { 598 int btcode = 0; 599 switch (i) { 600 #define EACH_SIG(ch, bt, ignore) \ 601 case ch: { btcode = bt; break; } 602 SIGNATURE_TYPES_DO(EACH_SIG, ignore) 603 #undef EACH_SIG 604 } 605 int btc = decode_signature_char(i); 606 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode); 607 } 608 return true; 609 } 610 611 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) { 612 const char* method_sig = (const char*)sig->bytes(); 613 ssize_t len = sig->utf8_length(); 614 ssize_t index = 0; 615 if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) { 616 ++index; 617 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) { 618 ssize_t res = is_valid_type(&method_sig[index], len - index); 619 if (res == -1) { 620 return false; 621 } else { 622 index += res; 623 } 624 } 625 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) { 626 // check the return type 627 ++index; 628 return (is_valid_type(&method_sig[index], len - index) == (len - index)); 629 } 630 } 631 return false; 632 } 633 634 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) { 635 const char* type_sig = (const char*)sig->bytes(); 636 ssize_t len = sig->utf8_length(); 637 return (type_sig != nullptr && len >= 1 && 638 (is_valid_type(type_sig, len) == len)); 639 } 640 641 // Checks to see if the type (not to go beyond 'limit') refers to a valid type. 642 // Returns -1 if it is not, or the index of the next character that is not part 643 // of the type. The type encoding may end before 'limit' and that's ok. 644 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) { 645 ssize_t index = 0; 646 647 // Iterate over any number of array dimensions 648 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index; 649 if (index >= limit) { 650 return -1; 651 } 652 switch (type[index]) { 653 case JVM_SIGNATURE_BYTE: 654 case JVM_SIGNATURE_CHAR: 655 case JVM_SIGNATURE_FLOAT: 656 case JVM_SIGNATURE_DOUBLE: 657 case JVM_SIGNATURE_INT: 658 case JVM_SIGNATURE_LONG: 659 case JVM_SIGNATURE_SHORT: 660 case JVM_SIGNATURE_BOOLEAN: 661 case JVM_SIGNATURE_VOID: 662 return index + 1; 663 case JVM_SIGNATURE_CLASS: 664 for (index = index + 1; index < limit; ++index) { 665 char c = type[index]; 666 switch (c) { 667 case JVM_SIGNATURE_ENDCLASS: 668 return index + 1; 669 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY: 670 return -1; 671 default: ; // fall through 672 } 673 } 674 // fall through 675 default: ; // fall through 676 } 677 return -1; 678 } 679 680 #endif // ASSERT 681 682 // Adds an argument to the signature 683 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset, float sort_offset) { 684 if (sort_offset == -1) { 685 sort_offset = offset; 686 } 687 sig->append(SigEntry(bt, offset, sort_offset, symbol)); 688 if (bt == T_LONG || bt == T_DOUBLE) { 689 sig->append(SigEntry(T_VOID, offset, sort_offset, symbol)); // Longs and doubles take two stack slots 690 } 691 } 692 693 // Returns true if the argument at index 'i' is not an inline type delimiter 694 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) { 695 return (sig->at(i)._bt != T_METADATA && 696 (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE)); 697 } 698 699 // Fill basic type array from signature array 700 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) { 701 int count = 0; 702 for (int i = 0; i < sig->length(); i++) { 703 if (skip_value_delimiters(sig, i)) { 704 sig_bt[count++] = sig->at(i)._bt; 705 } 706 } 707 return count; 708 } 709 710 // Create a temporary symbol from the signature array 711 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) { 712 ResourceMark rm; 713 int length = sig->length(); 714 char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3); 715 int idx = 0; 716 sig_str[idx++] = '('; 717 for (int i = 0; i < length; i++) { 718 BasicType bt = sig->at(i)._bt; 719 if (bt == T_METADATA || bt == T_VOID) { 720 // Ignore 721 } else { 722 if (bt == T_ARRAY) { 723 bt = T_OBJECT; // We don't know the element type, treat as Object 724 } 725 sig_str[idx++] = type2char(bt); 726 if (bt == T_OBJECT) { 727 sig_str[idx++] = ';'; 728 } 729 } 730 } 731 sig_str[idx++] = ')'; 732 // Add a dummy return type. It won't be used but SignatureStream needs it. 733 sig_str[idx++] = 'V'; 734 sig_str[idx++] = '\0'; 735 return SymbolTable::new_symbol(sig_str); 736 }