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