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 "asm/assembler.hpp"
27 #include "classfile/symbolTable.hpp"
28 #include "classfile/systemDictionary.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "memory/oopFactory.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "memory/universe.hpp"
33 #include "oops/instanceKlass.hpp"
34 #include "oops/klass.inline.hpp"
35 #include "oops/oop.inline.hpp"
36 #include "oops/symbol.hpp"
37 #include "oops/typeArrayKlass.hpp"
38 #include "runtime/fieldDescriptor.inline.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/safepointVerifiers.hpp"
41 #include "runtime/sharedRuntime.hpp"
42 #include "runtime/signature.hpp"
43 #include "runtime/sharedRuntime.hpp"
44
45 // Implementation of SignatureIterator
46
47 // Signature syntax:
48 //
49 // Signature = "(" {Parameter} ")" ReturnType.
50 // Parameter = FieldType.
51 // ReturnType = FieldType | "V".
52 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
53 // ClassName = string.
54
55 // The ClassName string can be any JVM-style UTF8 string except:
56 // - an empty string (the empty string is never a name of any kind)
57 // - a string which begins or ends with slash '/' (the package separator)
58 // - a string which contains adjacent slashes '//' (no empty package names)
59 // - a string which contains a semicolon ';' (the end-delimiter)
60 // - a string which contains a left bracket '[' (the array marker)
61 // - a string which contains a dot '.' (the external package separator)
62 //
63 // Other "meta-looking" characters, such as '(' and '<' and '+',
64 // are perfectly legitimate within a class name, for the JVM.
65 // Class names which contain double slashes ('a//b') and non-initial
66 // brackets ('a[b]') are reserved for possible enrichment of the
67 // type language.
68
69 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
70 if (!fp_is_valid(fingerprint)) {
71 _fingerprint = fingerprint;
72 _return_type = T_ILLEGAL;
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 defined(PPC64) || defined(S390)
239 if (_int_args < Argument::n_int_register_parameters_j) {
240 _int_args++;
241 } else {
242 _stack_arg_slots += 1;
243 }
244 break;
245 #endif // defined(PPC64) || defined(S390)
246 case T_LONG:
247 case T_OBJECT:
248 case T_ARRAY:
249 case T_ADDRESS:
250 if (_int_args < Argument::n_int_register_parameters_j) {
251 _int_args++;
252 } else {
253 PPC64_ONLY(_stack_arg_slots = align_up(_stack_arg_slots, 2));
254 S390_ONLY(_stack_arg_slots = align_up(_stack_arg_slots, 2));
255 _stack_arg_slots += 2;
256 }
257 break;
258 case T_FLOAT:
259 #if defined(PPC64) || defined(S390)
260 if (_fp_args < Argument::n_float_register_parameters_j) {
261 _fp_args++;
262 } else {
263 _stack_arg_slots += 1;
264 }
265 break;
266 #endif // defined(PPC64) || defined(S390)
267 case T_DOUBLE:
268 if (_fp_args < Argument::n_float_register_parameters_j) {
269 _fp_args++;
316 assert(_names == nullptr, "_names unexpectedly created");
317 return;
318 }
319
320 // decrement refcount for names created during signature parsing
321 _previous_name->decrement_refcount();
322 if (_names != nullptr) {
323 for (int i = 0; i < _names->length(); i++) {
324 _names->at(i)->decrement_refcount();
325 }
326 }
327 }
328
329 inline int SignatureStream::scan_type(BasicType type) {
330 const u1* base = _signature->bytes();
331 int end = _end;
332 int limit = _limit;
333 const u1* tem;
334 switch (type) {
335 case T_OBJECT:
336 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
337 return (tem == nullptr ? limit : tem + 1 - base);
338
339 case T_ARRAY:
340 while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; }
341 // If we discovered only the string of '[', this means something is wrong.
342 if (end >= limit) {
343 assert(false, "Invalid type detected");
344 return limit;
345 }
346 _array_prefix = end - _end; // number of '[' chars just skipped
347 if (Signature::has_envelope(base[end])) {
348 tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
349 return (tem == nullptr ? limit : tem + 1 - base);
350 }
351 // Skipping over a single character for a primitive type.
352 assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected");
353 return end + 1;
354
355 default:
400 assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
401 // The first character is already checked
402 int i = 1;
403 int len = sig->utf8_length();
404 // First skip all '['s
405 while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
406
407 // Check type
408 switch(sig->char_at(i)) {
409 case JVM_SIGNATURE_BYTE:
410 case JVM_SIGNATURE_CHAR:
411 case JVM_SIGNATURE_DOUBLE:
412 case JVM_SIGNATURE_FLOAT:
413 case JVM_SIGNATURE_INT:
414 case JVM_SIGNATURE_LONG:
415 case JVM_SIGNATURE_SHORT:
416 case JVM_SIGNATURE_BOOLEAN:
417 // If it is an array, the type is the last character
418 return (i + 1 == len);
419 case JVM_SIGNATURE_CLASS:
420 // If it is an object, the last character must be a ';'
421 return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
422 }
423 return false;
424 }
425
426 BasicType Signature::basic_type(int ch) {
427 BasicType btcode = decode_signature_char(ch);
428 if (btcode == 0) return T_ILLEGAL;
429 return btcode;
430 }
431
432 Symbol* Signature::strip_envelope(const Symbol* signature) {
433 assert(has_envelope(signature), "precondition");
434 return SymbolTable::new_symbol((char*) signature->bytes() + 1,
435 signature->utf8_length() - 2);
436 }
437
438 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
439 static const char jl_str[] = "java/lang/";
481 if (name->equals(symbol_chars, len)) {
482 return name;
483 }
484
485 // Save names for cleaning up reference count at the end of
486 // SignatureStream scope.
487 name = SymbolTable::new_symbol(symbol_chars, len);
488
489 // Only allocate the GrowableArray for the _names buffer if more than
490 // one name is being processed in the signature.
491 if (!_previous_name->is_permanent()) {
492 if (_names == nullptr) {
493 _names = new GrowableArray<Symbol*>(10);
494 }
495 _names->push(_previous_name);
496 }
497 _previous_name = name;
498 return name;
499 }
500
501 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
502 FailureMode failure_mode, TRAPS) {
503 if (!is_reference()) {
504 return nullptr;
505 }
506 Symbol* name = as_symbol();
507 Klass* k = nullptr;
508 if (failure_mode == ReturnNull) {
509 // Note: SD::resolve_or_null returns null for most failure modes,
510 // but not all. Circularity errors, invalid PDs, etc., throw.
511 k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
512 } else if (failure_mode == CachedOrNull) {
513 NoSafepointVerifier nsv; // no loading, now, we mean it!
514 assert(!HAS_PENDING_EXCEPTION, "");
515 k = SystemDictionary::find_instance_klass(THREAD, name, class_loader, protection_domain);
516 // SD::find does not trigger loading, so there should be no throws
517 // Still, bad things can happen, so we CHECK_NULL and ask callers
518 // to do likewise.
519 return k;
520 } else {
521 // The only remaining failure mode is NCDFError.
522 // The test here allows for an additional mode CNFException
523 // if callers need to request the reflective error instead.
524 bool throw_error = (failure_mode == NCDFError);
525 k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
526 }
527
528 return k;
529 }
530
531 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
532 FailureMode failure_mode, TRAPS) {
533 if (!is_reference()) {
534 return Universe::java_mirror(type());
535 }
536 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
537 if (klass == nullptr) {
538 return nullptr;
539 }
540 return klass->java_mirror();
541 }
542
543 void SignatureStream::skip_to_return_type() {
544 while (!at_return_type()) {
545 next();
546 }
547 }
548
549 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
550 Handle class_loader,
551 Handle protection_domain,
552 bool is_method)
553 : SignatureStream(signature, is_method),
554 _class_loader(class_loader), _protection_domain(protection_domain)
555 {
556 initialize_load_origin(nullptr);
557 }
558
559 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
560 : SignatureStream(signature, is_method)
561 {
562 assert(load_origin != nullptr, "");
563 initialize_load_origin(load_origin);
564 }
565
566 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
567 : SignatureStream(method->signature(), true)
568 {
569 initialize_load_origin(method->method_holder());
570 }
571
572 void ResolvingSignatureStream::cache_handles() {
573 assert(_load_origin != nullptr, "");
574 JavaThread* current = JavaThread::current();
575 _class_loader = Handle(current, _load_origin->class_loader());
576 _protection_domain = Handle(current, _load_origin->protection_domain());
577 }
578
579 #ifdef ASSERT
580
581 extern bool signature_constants_sane(); // called from basic_types_init()
582
583 bool signature_constants_sane() {
584 // for the lookup table, test every 8-bit code point, and then some:
585 for (int i = -256; i <= 256; i++) {
586 int btcode = 0;
587 switch (i) {
588 #define EACH_SIG(ch, bt, ignore) \
589 case ch: { btcode = bt; break; }
590 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
591 #undef EACH_SIG
592 }
593 int btc = decode_signature_char(i);
594 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
595 }
596 return true;
597 }
598
599 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
600 const char* method_sig = (const char*)sig->bytes();
601 ssize_t len = sig->utf8_length();
602 ssize_t index = 0;
603 if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
604 ++index;
605 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
606 ssize_t res = is_valid_type(&method_sig[index], len - index);
607 if (res == -1) {
608 return false;
609 } else {
610 index += res;
611 }
612 }
613 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
614 // check the return type
615 ++index;
616 return (is_valid_type(&method_sig[index], len - index) == (len - index));
617 }
618 }
619 return false;
620 }
621
622 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
623 const char* type_sig = (const char*)sig->bytes();
624 ssize_t len = sig->utf8_length();
625 return (type_sig != nullptr && len >= 1 &&
626 (is_valid_type(type_sig, len) == len));
627 }
628
629 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
630 // Returns -1 if it is not, or the index of the next character that is not part
631 // of the type. The type encoding may end before 'limit' and that's ok.
632 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
633 ssize_t index = 0;
634
635 // Iterate over any number of array dimensions
636 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
637 if (index >= limit) {
638 return -1;
639 }
640 switch (type[index]) {
641 case JVM_SIGNATURE_BYTE:
642 case JVM_SIGNATURE_CHAR:
643 case JVM_SIGNATURE_FLOAT:
644 case JVM_SIGNATURE_DOUBLE:
645 case JVM_SIGNATURE_INT:
646 case JVM_SIGNATURE_LONG:
647 case JVM_SIGNATURE_SHORT:
648 case JVM_SIGNATURE_BOOLEAN:
649 case JVM_SIGNATURE_VOID:
650 return index + 1;
651 case JVM_SIGNATURE_CLASS:
652 for (index = index + 1; index < limit; ++index) {
653 char c = type[index];
654 switch (c) {
655 case JVM_SIGNATURE_ENDCLASS:
656 return index + 1;
657 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
658 return -1;
659 default: ; // fall through
660 }
661 }
662 // fall through
663 default: ; // fall through
664 }
665 return -1;
666 }
667
668 #endif // ASSERT
|
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 "asm/assembler.hpp"
27 #include "classfile/symbolTable.hpp"
28 #include "classfile/systemDictionary.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "memory/oopFactory.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "memory/universe.hpp"
33 #include "oops/instanceKlass.hpp"
34 #include "oops/klass.inline.hpp"
35 #include "oops/oop.inline.hpp"
36 #include "oops/symbol.hpp"
37 #include "oops/typeArrayKlass.hpp"
38 #include "oops/inlineKlass.inline.hpp"
39 #include "runtime/fieldDescriptor.inline.hpp"
40 #include "runtime/handles.inline.hpp"
41 #include "runtime/interfaceSupport.inline.hpp"
42 #include "runtime/safepointVerifiers.hpp"
43 #include "runtime/sharedRuntime.hpp"
44 #include "runtime/signature.hpp"
45 #include "runtime/sharedRuntime.hpp"
46
47 // Implementation of SignatureIterator
48
49 // Signature syntax:
50 //
51 // Signature = "(" {Parameter} ")" ReturnType.
52 // Parameter = FieldType.
53 // ReturnType = FieldType | "V".
54 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "Q" ValueClassName ";" | "[" FieldType.
55 // ClassName = string.
56
57 // The ClassName string can be any JVM-style UTF8 string except:
58 // - an empty string (the empty string is never a name of any kind)
59 // - a string which begins or ends with slash '/' (the package separator)
60 // - a string which contains adjacent slashes '//' (no empty package names)
61 // - a string which contains a semicolon ';' (the end-delimiter)
62 // - a string which contains a left bracket '[' (the array marker)
63 // - a string which contains a dot '.' (the external package separator)
64 //
65 // Other "meta-looking" characters, such as '(' and '<' and '+',
66 // are perfectly legitimate within a class name, for the JVM.
67 // Class names which contain double slashes ('a//b') and non-initial
68 // brackets ('a[b]') are reserved for possible enrichment of the
69 // type language.
70
71 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
72 if (!fp_is_valid(fingerprint)) {
73 _fingerprint = fingerprint;
74 _return_type = T_ILLEGAL;
232 switch (type) {
233 case T_VOID:
234 break;
235 case T_BOOLEAN:
236 case T_CHAR:
237 case T_BYTE:
238 case T_SHORT:
239 case T_INT:
240 #if defined(PPC64) || defined(S390)
241 if (_int_args < Argument::n_int_register_parameters_j) {
242 _int_args++;
243 } else {
244 _stack_arg_slots += 1;
245 }
246 break;
247 #endif // defined(PPC64) || defined(S390)
248 case T_LONG:
249 case T_OBJECT:
250 case T_ARRAY:
251 case T_ADDRESS:
252 case T_PRIMITIVE_OBJECT:
253 if (_int_args < Argument::n_int_register_parameters_j) {
254 _int_args++;
255 } else {
256 PPC64_ONLY(_stack_arg_slots = align_up(_stack_arg_slots, 2));
257 S390_ONLY(_stack_arg_slots = align_up(_stack_arg_slots, 2));
258 _stack_arg_slots += 2;
259 }
260 break;
261 case T_FLOAT:
262 #if defined(PPC64) || defined(S390)
263 if (_fp_args < Argument::n_float_register_parameters_j) {
264 _fp_args++;
265 } else {
266 _stack_arg_slots += 1;
267 }
268 break;
269 #endif // defined(PPC64) || defined(S390)
270 case T_DOUBLE:
271 if (_fp_args < Argument::n_float_register_parameters_j) {
272 _fp_args++;
319 assert(_names == nullptr, "_names unexpectedly created");
320 return;
321 }
322
323 // decrement refcount for names created during signature parsing
324 _previous_name->decrement_refcount();
325 if (_names != nullptr) {
326 for (int i = 0; i < _names->length(); i++) {
327 _names->at(i)->decrement_refcount();
328 }
329 }
330 }
331
332 inline int SignatureStream::scan_type(BasicType type) {
333 const u1* base = _signature->bytes();
334 int end = _end;
335 int limit = _limit;
336 const u1* tem;
337 switch (type) {
338 case T_OBJECT:
339 case T_PRIMITIVE_OBJECT:
340 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
341 return (tem == nullptr ? limit : 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 : 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:
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 case JVM_SIGNATURE_PRIMITIVE_OBJECT:
425 // If it is an object, the last character must be a ';'
426 return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
427 }
428 return false;
429 }
430
431 BasicType Signature::basic_type(int ch) {
432 BasicType btcode = decode_signature_char(ch);
433 if (btcode == 0) return T_ILLEGAL;
434 return btcode;
435 }
436
437 Symbol* Signature::strip_envelope(const Symbol* signature) {
438 assert(has_envelope(signature), "precondition");
439 return SymbolTable::new_symbol((char*) signature->bytes() + 1,
440 signature->utf8_length() - 2);
441 }
442
443 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
444 static const char jl_str[] = "java/lang/";
486 if (name->equals(symbol_chars, len)) {
487 return name;
488 }
489
490 // Save names for cleaning up reference count at the end of
491 // SignatureStream scope.
492 name = SymbolTable::new_symbol(symbol_chars, len);
493
494 // Only allocate the GrowableArray for the _names buffer if more than
495 // one name is being processed in the signature.
496 if (!_previous_name->is_permanent()) {
497 if (_names == nullptr) {
498 _names = new GrowableArray<Symbol*>(10);
499 }
500 _names->push(_previous_name);
501 }
502 _previous_name = name;
503 return name;
504 }
505
506 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) {
507 ThreadInVMfromUnknown tiv;
508 JavaThread* THREAD = JavaThread::current();
509 HandleMark hm(THREAD);
510 Handle class_loader(THREAD, holder->class_loader());
511 Handle protection_domain(THREAD, holder->protection_domain());
512 Klass* k = as_klass(class_loader, protection_domain, SignatureStream::CachedOrNull, THREAD);
513 assert(!HAS_PENDING_EXCEPTION, "Should never throw");
514 if (k != NULL && k->is_inline_klass()) {
515 return InlineKlass::cast(k);
516 } else {
517 return NULL;
518 }
519 }
520
521 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
522 FailureMode failure_mode, TRAPS) {
523 if (!is_reference()) {
524 return nullptr;
525 }
526 Symbol* name = as_symbol();
527 Klass* k = nullptr;
528 if (failure_mode == ReturnNull) {
529 // Note: SD::resolve_or_null returns null for most failure modes,
530 // but not all. Circularity errors, invalid PDs, etc., throw.
531 k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
532 } else if (failure_mode == CachedOrNull) {
533 NoSafepointVerifier nsv; // no loading, now, we mean it!
534 assert(!HAS_PENDING_EXCEPTION, "");
535 k = SystemDictionary::find_instance_klass(THREAD, name, class_loader, protection_domain);
536 // SD::find does not trigger loading, so there should be no throws
537 // Still, bad things can happen, so we CHECK_NULL and ask callers
538 // to do likewise.
539 return k;
540 } else {
541 // The only remaining failure mode is NCDFError.
542 // The test here allows for an additional mode CNFException
543 // if callers need to request the reflective error instead.
544 bool throw_error = (failure_mode == NCDFError);
545 k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
546 }
547
548 return k;
549 }
550
551 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
552 FailureMode failure_mode, TRAPS) {
553 if (!is_reference()) {
554 return Universe::java_mirror(type());
555 }
556 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
557 if (klass == nullptr) {
558 return nullptr;
559 }
560 return has_Q_descriptor() ? InlineKlass::cast(klass)->val_mirror()
561 : klass->java_mirror();
562 }
563
564 void SignatureStream::skip_to_return_type() {
565 while (!at_return_type()) {
566 next();
567 }
568 }
569
570 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
571 Handle class_loader,
572 Handle protection_domain,
573 bool is_method)
574 : SignatureStream(signature, is_method),
575 _class_loader(class_loader), _protection_domain(protection_domain)
576 {
577 initialize_load_origin(nullptr);
578 }
579
580 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
581 : SignatureStream(signature, is_method)
582 {
583 assert(load_origin != nullptr, "");
584 initialize_load_origin(load_origin);
585 }
586
587 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
588 : SignatureStream(method->signature(), true)
589 {
590 initialize_load_origin(method->method_holder());
591 }
592
593 void ResolvingSignatureStream::cache_handles() {
594 assert(_load_origin != nullptr, "");
595 JavaThread* current = JavaThread::current();
596 _class_loader = Handle(current, _load_origin->class_loader());
597 _protection_domain = Handle(current, _load_origin->protection_domain());
598 }
599
600 #ifdef ASSERT
601 extern bool signature_constants_sane(); // called from basic_types_init()
602
603 bool signature_constants_sane() {
604 // for the lookup table, test every 8-bit code point, and then some:
605 for (int i = -256; i <= 256; i++) {
606 int btcode = 0;
607 switch (i) {
608 #define EACH_SIG(ch, bt, ignore) \
609 case ch: { btcode = bt; break; }
610 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
611 #undef EACH_SIG
612 }
613 int btc = decode_signature_char(i);
614 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
615 }
616 return true;
617 }
618
619 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
620 const char* method_sig = (const char*)sig->bytes();
621 ssize_t len = sig->utf8_length();
622 ssize_t index = 0;
623 if (method_sig != nullptr && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
624 ++index;
625 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
626 ssize_t res = is_valid_type(&method_sig[index], len - index);
627 if (res == -1) {
628 return false;
629 } else {
630 index += res;
631 }
632 }
633 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
634 // check the return type
635 ++index;
636 return (is_valid_type(&method_sig[index], len - index) == (len - index));
637 }
638 }
639 return false;
640 }
641
642 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
643 const char* type_sig = (const char*)sig->bytes();
644 ssize_t len = sig->utf8_length();
645 return (type_sig != nullptr && len >= 1 &&
646 (is_valid_type(type_sig, len) == len));
647 }
648
649 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
650 // Returns -1 if it is not, or the index of the next character that is not part
651 // of the type. The type encoding may end before 'limit' and that's ok.
652 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
653 ssize_t index = 0;
654
655 // Iterate over any number of array dimensions
656 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
657 if (index >= limit) {
658 return -1;
659 }
660 switch (type[index]) {
661 case JVM_SIGNATURE_BYTE:
662 case JVM_SIGNATURE_CHAR:
663 case JVM_SIGNATURE_FLOAT:
664 case JVM_SIGNATURE_DOUBLE:
665 case JVM_SIGNATURE_INT:
666 case JVM_SIGNATURE_LONG:
667 case JVM_SIGNATURE_SHORT:
668 case JVM_SIGNATURE_BOOLEAN:
669 case JVM_SIGNATURE_VOID:
670 return index + 1;
671 case JVM_SIGNATURE_PRIMITIVE_OBJECT: // fall through
672 case JVM_SIGNATURE_CLASS:
673 for (index = index + 1; index < limit; ++index) {
674 char c = type[index];
675 switch (c) {
676 case JVM_SIGNATURE_ENDCLASS:
677 return index + 1;
678 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
679 return -1;
680 default: ; // fall through
681 }
682 }
683 // fall through
684 default: ; // fall through
685 }
686 return -1;
687 }
688
689 #endif // ASSERT
690
691 // Adds an argument to the signature
692 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset) {
693 sig->append(SigEntry(bt, offset, symbol));
694 if (bt == T_LONG || bt == T_DOUBLE) {
695 sig->append(SigEntry(T_VOID, offset, symbol)); // Longs and doubles take two stack slots
696 }
697 }
698
699 // Returns true if the argument at index 'i' is not an inline type delimiter
700 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
701 return (sig->at(i)._bt != T_PRIMITIVE_OBJECT &&
702 (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
703 }
704
705 // Fill basic type array from signature array
706 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
707 int count = 0;
708 for (int i = 0; i < sig->length(); i++) {
709 if (skip_value_delimiters(sig, i)) {
710 sig_bt[count++] = sig->at(i)._bt;
711 }
712 }
713 return count;
714 }
715
716 // Create a temporary symbol from the signature array
717 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
718 ResourceMark rm;
719 int length = sig->length();
720 char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
721 int idx = 0;
722 sig_str[idx++] = '(';
723 for (int i = 0; i < length; i++) {
724 BasicType bt = sig->at(i)._bt;
725 if (bt == T_PRIMITIVE_OBJECT || bt == T_VOID) {
726 // Ignore
727 } else {
728 if (bt == T_ARRAY) {
729 bt = T_OBJECT; // We don't know the element type, treat as Object
730 }
731 sig_str[idx++] = type2char(bt);
732 if (bt == T_OBJECT) {
733 sig_str[idx++] = ';';
734 }
735 }
736 }
737 sig_str[idx++] = ')';
738 // Add a dummy return type. It won't be used but SignatureStream needs it.
739 sig_str[idx++] = 'V';
740 sig_str[idx++] = '\0';
741 return SymbolTable::new_symbol(sig_str);
742 }
|