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 "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 "runtime/fieldDescriptor.inline.hpp"
38 #include "runtime/handles.inline.hpp"
39 #include "runtime/safepointVerifiers.hpp"
40 #include "runtime/signature.hpp"
41
42 // Implementation of SignatureIterator
43
44 // Signature syntax:
45 //
46 // Signature = "(" {Parameter} ")" ReturnType.
47 // Parameter = FieldType.
48 // ReturnType = FieldType | "V".
49 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
50 // ClassName = string.
51
52 // The ClassName string can be any JVM-style UTF8 string except:
53 // - an empty string (the empty string is never a name of any kind)
54 // - a string which begins or ends with slash '/' (the package separator)
55 // - a string which contains adjacent slashes '//' (no empty package names)
56 // - a string which contains a semicolon ';' (the end-delimiter)
57 // - a string which contains a left bracket '[' (the array marker)
58 // - a string which contains a dot '.' (the external package separator)
59 //
60 // Other "meta-looking" characters, such as '(' and '<' and '+',
61 // are perfectly legitimate within a class name, for the JVM.
62 // Class names which contain double slashes ('a//b') and non-initial
63 // brackets ('a[b]') are reserved for possible enrichment of the
64 // type language.
65
66 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
67 if (!fp_is_valid(fingerprint)) {
68 _fingerprint = fingerprint;
69 _return_type = T_ILLEGAL;
208 assert(_names == NULL, "_names unexpectedly created");
209 return;
210 }
211
212 // decrement refcount for names created during signature parsing
213 _previous_name->decrement_refcount();
214 if (_names != NULL) {
215 for (int i = 0; i < _names->length(); i++) {
216 _names->at(i)->decrement_refcount();
217 }
218 }
219 }
220
221 inline int SignatureStream::scan_type(BasicType type) {
222 const u1* base = _signature->bytes();
223 int end = _end;
224 int limit = _limit;
225 const u1* tem;
226 switch (type) {
227 case T_OBJECT:
228 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
229 return (tem == NULL ? limit : tem + 1 - base);
230
231 case T_ARRAY:
232 while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; }
233 _array_prefix = end - _end; // number of '[' chars just skipped
234 if (Signature::has_envelope(base[end])) {
235 tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
236 return (tem == NULL ? limit : tem + 1 - base);
237 }
238 // Skipping over a single character for a primitive type.
239 assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected");
240 return end + 1;
241
242 default:
243 // Skipping over a single character for a primitive type (or void).
244 assert(!is_reference_type(type), "only primitives or void expected");
245 return end + 1;
246 }
247 }
287 assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
288 // The first character is already checked
289 int i = 1;
290 int len = sig->utf8_length();
291 // First skip all '['s
292 while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
293
294 // Check type
295 switch(sig->char_at(i)) {
296 case JVM_SIGNATURE_BYTE:
297 case JVM_SIGNATURE_CHAR:
298 case JVM_SIGNATURE_DOUBLE:
299 case JVM_SIGNATURE_FLOAT:
300 case JVM_SIGNATURE_INT:
301 case JVM_SIGNATURE_LONG:
302 case JVM_SIGNATURE_SHORT:
303 case JVM_SIGNATURE_BOOLEAN:
304 // If it is an array, the type is the last character
305 return (i + 1 == len);
306 case JVM_SIGNATURE_CLASS:
307 // If it is an object, the last character must be a ';'
308 return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
309 }
310 return false;
311 }
312
313 BasicType Signature::basic_type(int ch) {
314 BasicType btcode = decode_signature_char(ch);
315 if (btcode == 0) return T_ILLEGAL;
316 return btcode;
317 }
318
319 Symbol* Signature::strip_envelope(const Symbol* signature) {
320 assert(has_envelope(signature), "precondition");
321 return SymbolTable::new_symbol((char*) signature->bytes() + 1,
322 signature->utf8_length() - 2);
323 }
324
325 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
326 static const char jl_str[] = "java/lang/";
368 if (name->equals(symbol_chars, len)) {
369 return name;
370 }
371
372 // Save names for cleaning up reference count at the end of
373 // SignatureStream scope.
374 name = SymbolTable::new_symbol(symbol_chars, len);
375
376 // Only allocate the GrowableArray for the _names buffer if more than
377 // one name is being processed in the signature.
378 if (!_previous_name->is_permanent()) {
379 if (_names == NULL) {
380 _names = new GrowableArray<Symbol*>(10);
381 }
382 _names->push(_previous_name);
383 }
384 _previous_name = name;
385 return name;
386 }
387
388 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
389 FailureMode failure_mode, TRAPS) {
390 if (!is_reference()) {
391 return NULL;
392 }
393 Symbol* name = as_symbol();
394 Klass* k = NULL;
395 if (failure_mode == ReturnNull) {
396 // Note: SD::resolve_or_null returns NULL for most failure modes,
397 // but not all. Circularity errors, invalid PDs, etc., throw.
398 k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
399 } else if (failure_mode == CachedOrNull) {
400 NoSafepointVerifier nsv; // no loading, now, we mean it!
401 assert(!HAS_PENDING_EXCEPTION, "");
402 k = SystemDictionary::find_instance_klass(name, class_loader, protection_domain);
403 // SD::find does not trigger loading, so there should be no throws
404 // Still, bad things can happen, so we CHECK_NULL and ask callers
405 // to do likewise.
406 return k;
407 } else {
408 // The only remaining failure mode is NCDFError.
409 // The test here allows for an additional mode CNFException
410 // if callers need to request the reflective error instead.
411 bool throw_error = (failure_mode == NCDFError);
412 k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
413 }
414
415 return k;
416 }
417
418 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
419 FailureMode failure_mode, TRAPS) {
420 if (!is_reference()) {
421 return Universe::java_mirror(type());
422 }
423 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
424 if (klass == NULL) {
425 return NULL;
426 }
427 return klass->java_mirror();
428 }
429
430 void SignatureStream::skip_to_return_type() {
431 while (!at_return_type()) {
432 next();
433 }
434 }
435
436 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
437 Handle class_loader,
438 Handle protection_domain,
439 bool is_method)
440 : SignatureStream(signature, is_method),
441 _class_loader(class_loader), _protection_domain(protection_domain)
442 {
443 initialize_load_origin(NULL);
444 }
445
446 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
447 : SignatureStream(signature, is_method)
464
465 void ResolvingSignatureStream::cache_handles() {
466 assert(_load_origin != NULL, "");
467 JavaThread* current = JavaThread::current();
468 _class_loader = Handle(current, _load_origin->class_loader());
469 _protection_domain = Handle(current, _load_origin->protection_domain());
470 }
471
472 Klass* ResolvingSignatureStream::as_klass_if_loaded(TRAPS) {
473 Klass* klass = as_klass(CachedOrNull, THREAD);
474 // SD::find does not trigger loading, so there should be no throws
475 // Still, bad things can happen, so we CHECK_NULL and ask callers
476 // to do likewise.
477 if (HAS_PENDING_EXCEPTION) {
478 CLEAR_PENDING_EXCEPTION;
479 }
480 return klass;
481 }
482
483 #ifdef ASSERT
484
485 extern bool signature_constants_sane(); // called from basic_types_init()
486
487 bool signature_constants_sane() {
488 // for the lookup table, test every 8-bit code point, and then some:
489 for (int i = -256; i <= 256; i++) {
490 int btcode = 0;
491 switch (i) {
492 #define EACH_SIG(ch, bt, ignore) \
493 case ch: { btcode = bt; break; }
494 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
495 #undef EACH_SIG
496 }
497 int btc = decode_signature_char(i);
498 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
499 }
500 return true;
501 }
502
503 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
504 const char* method_sig = (const char*)sig->bytes();
505 ssize_t len = sig->utf8_length();
506 ssize_t index = 0;
507 if (method_sig != NULL && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
508 ++index;
509 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
510 ssize_t res = is_valid_type(&method_sig[index], len - index);
511 if (res == -1) {
512 return false;
513 } else {
514 index += res;
515 }
516 }
517 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
518 // check the return type
519 ++index;
520 return (is_valid_type(&method_sig[index], len - index) == (len - index));
521 }
522 }
523 return false;
524 }
525
526 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
527 const char* type_sig = (const char*)sig->bytes();
528 ssize_t len = sig->utf8_length();
529 return (type_sig != NULL && len >= 1 &&
530 (is_valid_type(type_sig, len) == len));
531 }
532
533 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
534 // Returns -1 if it is not, or the index of the next character that is not part
535 // of the type. The type encoding may end before 'limit' and that's ok.
536 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
537 ssize_t index = 0;
538
539 // Iterate over any number of array dimensions
540 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
541 if (index >= limit) {
542 return -1;
543 }
544 switch (type[index]) {
545 case JVM_SIGNATURE_BYTE:
546 case JVM_SIGNATURE_CHAR:
547 case JVM_SIGNATURE_FLOAT:
548 case JVM_SIGNATURE_DOUBLE:
549 case JVM_SIGNATURE_INT:
550 case JVM_SIGNATURE_LONG:
551 case JVM_SIGNATURE_SHORT:
552 case JVM_SIGNATURE_BOOLEAN:
553 case JVM_SIGNATURE_VOID:
554 return index + 1;
555 case JVM_SIGNATURE_CLASS:
556 for (index = index + 1; index < limit; ++index) {
557 char c = type[index];
558 switch (c) {
559 case JVM_SIGNATURE_ENDCLASS:
560 return index + 1;
561 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
562 return -1;
563 default: ; // fall through
564 }
565 }
566 // fall through
567 default: ; // fall through
568 }
569 return -1;
570 }
571
572 #endif // ASSERT
|
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 "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/safepointVerifiers.hpp"
41 #include "runtime/signature.hpp"
42
43 // Implementation of SignatureIterator
44
45 // Signature syntax:
46 //
47 // Signature = "(" {Parameter} ")" ReturnType.
48 // Parameter = FieldType.
49 // ReturnType = FieldType | "V".
50 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "Q" ValueClassName ";" | "[" FieldType.
51 // ClassName = string.
52
53 // The ClassName string can be any JVM-style UTF8 string except:
54 // - an empty string (the empty string is never a name of any kind)
55 // - a string which begins or ends with slash '/' (the package separator)
56 // - a string which contains adjacent slashes '//' (no empty package names)
57 // - a string which contains a semicolon ';' (the end-delimiter)
58 // - a string which contains a left bracket '[' (the array marker)
59 // - a string which contains a dot '.' (the external package separator)
60 //
61 // Other "meta-looking" characters, such as '(' and '<' and '+',
62 // are perfectly legitimate within a class name, for the JVM.
63 // Class names which contain double slashes ('a//b') and non-initial
64 // brackets ('a[b]') are reserved for possible enrichment of the
65 // type language.
66
67 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
68 if (!fp_is_valid(fingerprint)) {
69 _fingerprint = fingerprint;
70 _return_type = T_ILLEGAL;
209 assert(_names == NULL, "_names unexpectedly created");
210 return;
211 }
212
213 // decrement refcount for names created during signature parsing
214 _previous_name->decrement_refcount();
215 if (_names != NULL) {
216 for (int i = 0; i < _names->length(); i++) {
217 _names->at(i)->decrement_refcount();
218 }
219 }
220 }
221
222 inline int SignatureStream::scan_type(BasicType type) {
223 const u1* base = _signature->bytes();
224 int end = _end;
225 int limit = _limit;
226 const u1* tem;
227 switch (type) {
228 case T_OBJECT:
229 case T_PRIMITIVE_OBJECT:
230 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
231 return (tem == NULL ? limit : tem + 1 - base);
232
233 case T_ARRAY:
234 while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; }
235 _array_prefix = end - _end; // number of '[' chars just skipped
236 if (Signature::has_envelope(base[end])) {
237 tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
238 return (tem == NULL ? limit : tem + 1 - base);
239 }
240 // Skipping over a single character for a primitive type.
241 assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected");
242 return end + 1;
243
244 default:
245 // Skipping over a single character for a primitive type (or void).
246 assert(!is_reference_type(type), "only primitives or void expected");
247 return end + 1;
248 }
249 }
289 assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
290 // The first character is already checked
291 int i = 1;
292 int len = sig->utf8_length();
293 // First skip all '['s
294 while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
295
296 // Check type
297 switch(sig->char_at(i)) {
298 case JVM_SIGNATURE_BYTE:
299 case JVM_SIGNATURE_CHAR:
300 case JVM_SIGNATURE_DOUBLE:
301 case JVM_SIGNATURE_FLOAT:
302 case JVM_SIGNATURE_INT:
303 case JVM_SIGNATURE_LONG:
304 case JVM_SIGNATURE_SHORT:
305 case JVM_SIGNATURE_BOOLEAN:
306 // If it is an array, the type is the last character
307 return (i + 1 == len);
308 case JVM_SIGNATURE_CLASS:
309 case JVM_SIGNATURE_PRIMITIVE_OBJECT:
310 // If it is an object, the last character must be a ';'
311 return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
312 }
313 return false;
314 }
315
316 BasicType Signature::basic_type(int ch) {
317 BasicType btcode = decode_signature_char(ch);
318 if (btcode == 0) return T_ILLEGAL;
319 return btcode;
320 }
321
322 Symbol* Signature::strip_envelope(const Symbol* signature) {
323 assert(has_envelope(signature), "precondition");
324 return SymbolTable::new_symbol((char*) signature->bytes() + 1,
325 signature->utf8_length() - 2);
326 }
327
328 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
329 static const char jl_str[] = "java/lang/";
371 if (name->equals(symbol_chars, len)) {
372 return name;
373 }
374
375 // Save names for cleaning up reference count at the end of
376 // SignatureStream scope.
377 name = SymbolTable::new_symbol(symbol_chars, len);
378
379 // Only allocate the GrowableArray for the _names buffer if more than
380 // one name is being processed in the signature.
381 if (!_previous_name->is_permanent()) {
382 if (_names == NULL) {
383 _names = new GrowableArray<Symbol*>(10);
384 }
385 _names->push(_previous_name);
386 }
387 _previous_name = name;
388 return name;
389 }
390
391 InlineKlass* SignatureStream::as_inline_klass(InstanceKlass* holder) {
392 JavaThread* THREAD = JavaThread::current();
393 Handle class_loader(THREAD, holder->class_loader());
394 Handle protection_domain(THREAD, holder->protection_domain());
395 Klass* k = as_klass(class_loader, protection_domain, SignatureStream::CachedOrNull, THREAD);
396 assert(!HAS_PENDING_EXCEPTION, "Should never throw");
397 if (k != NULL && k->is_inline_klass()) {
398 return InlineKlass::cast(k);
399 } else {
400 return NULL;
401 }
402 }
403
404 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
405 FailureMode failure_mode, TRAPS) {
406 if (!is_reference()) {
407 return NULL;
408 }
409 Symbol* name = as_symbol();
410 Klass* k = NULL;
411 if (failure_mode == ReturnNull) {
412 // Note: SD::resolve_or_null returns NULL for most failure modes,
413 // but not all. Circularity errors, invalid PDs, etc., throw.
414 k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
415 } else if (failure_mode == CachedOrNull) {
416 NoSafepointVerifier nsv; // no loading, now, we mean it!
417 assert(!HAS_PENDING_EXCEPTION, "");
418 k = SystemDictionary::find_instance_klass(name, class_loader, protection_domain);
419 // SD::find does not trigger loading, so there should be no throws
420 // Still, bad things can happen, so we CHECK_NULL and ask callers
421 // to do likewise.
422 return k;
423 } else {
424 // The only remaining failure mode is NCDFError.
425 // The test here allows for an additional mode CNFException
426 // if callers need to request the reflective error instead.
427 bool throw_error = (failure_mode == NCDFError);
428 k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
429 }
430
431 return k;
432 }
433
434 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
435 FailureMode failure_mode, TRAPS) {
436 if (!is_reference()) {
437 return Universe::java_mirror(type());
438 }
439 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
440 if (klass == NULL) {
441 return NULL;
442 }
443 return has_Q_descriptor() ? InlineKlass::cast(klass)->val_mirror()
444 : klass->java_mirror();
445 }
446
447 void SignatureStream::skip_to_return_type() {
448 while (!at_return_type()) {
449 next();
450 }
451 }
452
453 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
454 Handle class_loader,
455 Handle protection_domain,
456 bool is_method)
457 : SignatureStream(signature, is_method),
458 _class_loader(class_loader), _protection_domain(protection_domain)
459 {
460 initialize_load_origin(NULL);
461 }
462
463 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
464 : SignatureStream(signature, is_method)
481
482 void ResolvingSignatureStream::cache_handles() {
483 assert(_load_origin != NULL, "");
484 JavaThread* current = JavaThread::current();
485 _class_loader = Handle(current, _load_origin->class_loader());
486 _protection_domain = Handle(current, _load_origin->protection_domain());
487 }
488
489 Klass* ResolvingSignatureStream::as_klass_if_loaded(TRAPS) {
490 Klass* klass = as_klass(CachedOrNull, THREAD);
491 // SD::find does not trigger loading, so there should be no throws
492 // Still, bad things can happen, so we CHECK_NULL and ask callers
493 // to do likewise.
494 if (HAS_PENDING_EXCEPTION) {
495 CLEAR_PENDING_EXCEPTION;
496 }
497 return klass;
498 }
499
500 #ifdef ASSERT
501 extern bool signature_constants_sane(); // called from basic_types_init()
502
503 bool signature_constants_sane() {
504 // for the lookup table, test every 8-bit code point, and then some:
505 for (int i = -256; i <= 256; i++) {
506 int btcode = 0;
507 switch (i) {
508 #define EACH_SIG(ch, bt, ignore) \
509 case ch: { btcode = bt; break; }
510 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
511 #undef EACH_SIG
512 }
513 int btc = decode_signature_char(i);
514 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
515 }
516 return true;
517 }
518
519 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
520 const char* method_sig = (const char*)sig->bytes();
521 ssize_t len = sig->utf8_length();
522 ssize_t index = 0;
523 if (method_sig != NULL && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
524 ++index;
525 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
526 ssize_t res = is_valid_type(&method_sig[index], len - index);
527 if (res == -1) {
528 return false;
529 } else {
530 index += res;
531 }
532 }
533 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
534 // check the return type
535 ++index;
536 return (is_valid_type(&method_sig[index], len - index) == (len - index));
537 }
538 }
539 return false;
540 }
541
542 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
543 const char* type_sig = (const char*)sig->bytes();
544 ssize_t len = sig->utf8_length();
545 return (type_sig != NULL && len >= 1 &&
546 (is_valid_type(type_sig, len) == len));
547 }
548
549 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
550 // Returns -1 if it is not, or the index of the next character that is not part
551 // of the type. The type encoding may end before 'limit' and that's ok.
552 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
553 ssize_t index = 0;
554
555 // Iterate over any number of array dimensions
556 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
557 if (index >= limit) {
558 return -1;
559 }
560 switch (type[index]) {
561 case JVM_SIGNATURE_BYTE:
562 case JVM_SIGNATURE_CHAR:
563 case JVM_SIGNATURE_FLOAT:
564 case JVM_SIGNATURE_DOUBLE:
565 case JVM_SIGNATURE_INT:
566 case JVM_SIGNATURE_LONG:
567 case JVM_SIGNATURE_SHORT:
568 case JVM_SIGNATURE_BOOLEAN:
569 case JVM_SIGNATURE_VOID:
570 return index + 1;
571 case JVM_SIGNATURE_PRIMITIVE_OBJECT: // fall through
572 case JVM_SIGNATURE_CLASS:
573 for (index = index + 1; index < limit; ++index) {
574 char c = type[index];
575 switch (c) {
576 case JVM_SIGNATURE_ENDCLASS:
577 return index + 1;
578 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
579 return -1;
580 default: ; // fall through
581 }
582 }
583 // fall through
584 default: ; // fall through
585 }
586 return -1;
587 }
588
589 #endif // ASSERT
590
591 // Adds an argument to the signature
592 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset) {
593 sig->append(SigEntry(bt, offset, symbol));
594 if (bt == T_LONG || bt == T_DOUBLE) {
595 sig->append(SigEntry(T_VOID, offset, symbol)); // Longs and doubles take two stack slots
596 }
597 }
598
599 // Returns true if the argument at index 'i' is not an inline type delimiter
600 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
601 return (sig->at(i)._bt != T_PRIMITIVE_OBJECT &&
602 (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
603 }
604
605 // Fill basic type array from signature array
606 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
607 int count = 0;
608 for (int i = 0; i < sig->length(); i++) {
609 if (skip_value_delimiters(sig, i)) {
610 sig_bt[count++] = sig->at(i)._bt;
611 }
612 }
613 return count;
614 }
615
616 // Create a temporary symbol from the signature array
617 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
618 ResourceMark rm;
619 int length = sig->length();
620 char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
621 int idx = 0;
622 sig_str[idx++] = '(';
623 for (int i = 0; i < length; i++) {
624 BasicType bt = sig->at(i)._bt;
625 if (bt == T_PRIMITIVE_OBJECT || bt == T_VOID) {
626 // Ignore
627 } else {
628 if (bt == T_ARRAY) {
629 bt = T_OBJECT; // We don't know the element type, treat as Object
630 }
631 sig_str[idx++] = type2char(bt);
632 if (bt == T_OBJECT) {
633 sig_str[idx++] = ';';
634 }
635 }
636 }
637 sig_str[idx++] = ')';
638 // Add a dummy return type. It won't be used but SignatureStream needs it.
639 sig_str[idx++] = 'V';
640 sig_str[idx++] = '\0';
641 return SymbolTable::new_symbol(sig_str);
642 }
|