1 /*
2 * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/symbolTable.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "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
set_fingerprint(fingerprint_t fingerprint)66 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
67 if (!fp_is_valid(fingerprint)) {
68 _fingerprint = fingerprint;
69 _return_type = T_ILLEGAL;
70 } else if (fingerprint != _fingerprint) {
71 assert(_fingerprint == zero_fingerprint(), "consistent fingerprint values");
72 _fingerprint = fingerprint;
73 _return_type = fp_return_type(fingerprint);
74 }
75 }
76
return_type()77 BasicType SignatureIterator::return_type() {
78 if (_return_type == T_ILLEGAL) {
79 SignatureStream ss(_signature);
80 ss.skip_to_return_type();
81 _return_type = ss.type();
82 assert(_return_type != T_ILLEGAL, "illegal return type");
83 }
84 return _return_type;
85 }
86
fp_is_valid_type(BasicType type,bool for_return_type)87 bool SignatureIterator::fp_is_valid_type(BasicType type, bool for_return_type) {
88 assert(type != (BasicType)fp_parameters_done, "fingerprint is incorrectly at done");
89 assert(((int)type & ~fp_parameter_feature_mask) == 0, "fingerprint feature mask yielded non-zero value");
90 return (is_java_primitive(type) ||
91 is_reference_type(type) ||
92 (for_return_type && type == T_VOID));
93 }
94
ArgumentSizeComputer(Symbol * signature)95 ArgumentSizeComputer::ArgumentSizeComputer(Symbol* signature)
96 : SignatureIterator(signature)
97 {
98 _size = 0;
99 do_parameters_on(this); // non-virtual template execution
100 }
101
ArgumentCount(Symbol * signature)102 ArgumentCount::ArgumentCount(Symbol* signature)
103 : SignatureIterator(signature)
104 {
105 _size = 0;
106 do_parameters_on(this); // non-virtual template execution
107 }
108
ReferenceArgumentCount(Symbol * signature)109 ReferenceArgumentCount::ReferenceArgumentCount(Symbol* signature)
110 : SignatureIterator(signature)
111 {
112 _refs = 0;
113 do_parameters_on(this); // non-virtual template execution
114 }
115
compute_fingerprint_and_return_type(bool static_flag)116 void Fingerprinter::compute_fingerprint_and_return_type(bool static_flag) {
117 // See if we fingerprinted this method already
118 if (_method != NULL) {
119 assert(!static_flag, "must not be passed by caller");
120 static_flag = _method->is_static();
121 _fingerprint = _method->constMethod()->fingerprint();
122
123 if (_fingerprint != zero_fingerprint()) {
124 _return_type = _method->result_type();
125 assert(is_java_type(_return_type), "return type must be a java type");
126 return;
127 }
128
129 if (_method->size_of_parameters() > fp_max_size_of_parameters) {
130 _fingerprint = overflow_fingerprint();
131 _method->constMethod()->set_fingerprint(_fingerprint);
132 // as long as we are here compute the return type:
133 _return_type = ResultTypeFinder(_method->signature()).type();
134 assert(is_java_type(_return_type), "return type must be a java type");
135 return;
136 }
137 }
138
139 // Note: This will always take the slow path, since _fp==zero_fp.
140 initialize_accumulator();
141 do_parameters_on(this);
142 assert(fp_is_valid_type(_return_type, true), "bad result type");
143
144 // Fill in the return type and static bits:
145 _accumulator |= _return_type << fp_static_feature_size;
146 if (static_flag) {
147 _accumulator |= fp_is_static_bit;
148 } else {
149 _param_size += 1; // this is the convention for Method::compute_size_of_parameters
150 }
151
152 // Detect overflow. (We counted _param_size correctly.)
153 if (_method == NULL && _param_size > fp_max_size_of_parameters) {
154 // We did a one-pass computation of argument size, return type,
155 // and fingerprint.
156 _fingerprint = overflow_fingerprint();
157 return;
158 }
159
160 assert(_shift_count < BitsPerLong,
161 "shift count overflow %d (%d vs. %d): %s",
162 _shift_count, _param_size, fp_max_size_of_parameters,
163 _signature->as_C_string());
164 assert((_accumulator >> _shift_count) == fp_parameters_done, "must be zero");
165
166 // This is the result, along with _return_type:
167 _fingerprint = _accumulator;
168
169 // Cache the result on the method itself:
170 if (_method != NULL) {
171 _method->constMethod()->set_fingerprint(_fingerprint);
172 }
173 }
174
175 // Implementation of SignatureStream
176
decode_signature_char(int ch)177 static inline BasicType decode_signature_char(int ch) {
178 switch (ch) {
179 #define EACH_SIG(ch, bt, ignore) \
180 case ch: return bt;
181 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
182 #undef EACH_SIG
183 }
184 return (BasicType)0;
185 }
186
SignatureStream(const Symbol * signature,bool is_method)187 SignatureStream::SignatureStream(const Symbol* signature,
188 bool is_method) {
189 assert(!is_method || signature->starts_with(JVM_SIGNATURE_FUNC),
190 "method signature required");
191 _signature = signature;
192 _limit = signature->utf8_length();
193 int oz = (is_method ? _s_method : _s_field);
194 _state = oz;
195 _begin = _end = oz; // skip first '(' in method signatures
196 _array_prefix = 0; // just for definiteness
197
198 // assigning java/lang/Object to _previous_name means we can
199 // avoid a number of NULL checks in the parser
200 _previous_name = vmSymbols::java_lang_Object();
201 _names = NULL;
202 next();
203 }
204
~SignatureStream()205 SignatureStream::~SignatureStream() {
206 // decrement refcount for names created during signature parsing
207 _previous_name->decrement_refcount();
208 if (_names != NULL) {
209 for (int i = 0; i < _names->length(); i++) {
210 _names->at(i)->decrement_refcount();
211 }
212 }
213 }
214
scan_type(BasicType type)215 inline int SignatureStream::scan_type(BasicType type) {
216 const u1* base = _signature->bytes();
217 int end = _end;
218 int limit = _limit;
219 const u1* tem;
220 switch (type) {
221 case T_OBJECT:
222 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
223 return (tem == NULL ? limit : tem + 1 - base);
224
225 case T_ARRAY:
226 while ((end < limit) && ((char)base[end] == JVM_SIGNATURE_ARRAY)) { end++; }
227 _array_prefix = end - _end; // number of '[' chars just skipped
228 if (Signature::has_envelope(base[end])) {
229 tem = (const u1 *) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
230 return (tem == NULL ? limit : tem + 1 - base);
231 }
232 // Skipping over a single character for a primitive type.
233 assert(is_java_primitive(decode_signature_char(base[end])), "only primitives expected");
234 return end + 1;
235
236 default:
237 // Skipping over a single character for a primitive type (or void).
238 assert(!is_reference_type(type), "only primitives or void expected");
239 return end + 1;
240 }
241 }
242
next()243 void SignatureStream::next() {
244 const Symbol* sig = _signature;
245 int len = _limit;
246 if (_end >= len) { set_done(); return; }
247 _begin = _end;
248 int ch = sig->char_at(_begin);
249 if (ch == JVM_SIGNATURE_ENDFUNC) {
250 assert(_state == _s_method, "must be in method");
251 _state = _s_method_return;
252 _begin = ++_end;
253 if (_end >= len) { set_done(); return; }
254 ch = sig->char_at(_begin);
255 }
256 BasicType bt = decode_signature_char(ch);
257 assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch);
258 _type = bt;
259 _end = scan_type(bt);
260 }
261
skip_whole_array_prefix()262 int SignatureStream::skip_whole_array_prefix() {
263 assert(_type == T_ARRAY, "must be");
264
265 // we are stripping all levels of T_ARRAY,
266 // so we must decode the next character
267 int whole_array_prefix = _array_prefix;
268 int new_begin = _begin + whole_array_prefix;
269 _begin = new_begin;
270 int ch = _signature->char_at(new_begin);
271 BasicType bt = decode_signature_char(ch);
272 assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch);
273 _type = bt;
274 assert(bt != T_VOID && bt != T_ARRAY, "bad signature type");
275 // Don't bother to re-scan, since it won't change the value of _end.
276 return whole_array_prefix;
277 }
278
is_valid_array_signature(const Symbol * sig)279 bool Signature::is_valid_array_signature(const Symbol* sig) {
280 assert(sig->utf8_length() > 1, "this should already have been checked");
281 assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
282 // The first character is already checked
283 int i = 1;
284 int len = sig->utf8_length();
285 // First skip all '['s
286 while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
287
288 // Check type
289 switch(sig->char_at(i)) {
290 case JVM_SIGNATURE_BYTE:
291 case JVM_SIGNATURE_CHAR:
292 case JVM_SIGNATURE_DOUBLE:
293 case JVM_SIGNATURE_FLOAT:
294 case JVM_SIGNATURE_INT:
295 case JVM_SIGNATURE_LONG:
296 case JVM_SIGNATURE_SHORT:
297 case JVM_SIGNATURE_BOOLEAN:
298 // If it is an array, the type is the last character
299 return (i + 1 == len);
300 case JVM_SIGNATURE_CLASS:
301 // If it is an object, the last character must be a ';'
302 return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
303 }
304 return false;
305 }
306
basic_type(int ch)307 BasicType Signature::basic_type(int ch) {
308 BasicType btcode = decode_signature_char(ch);
309 if (btcode == 0) return T_ILLEGAL;
310 return btcode;
311 }
312
strip_envelope(const Symbol * signature)313 Symbol* Signature::strip_envelope(const Symbol* signature) {
314 assert(has_envelope(signature), "precondition");
315 return SymbolTable::new_symbol((char*) signature->bytes() + 1,
316 signature->utf8_length() - 2);
317 }
318
319 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
320 static const char jl_str[] = "java/lang/";
321
322 #ifdef ASSERT
signature_symbols_sane()323 static bool signature_symbols_sane() {
324 static bool done;
325 if (done) return true;
326 done = true;
327 // test some tense code that looks for common symbol names:
328 assert(vmSymbols::java_lang_Object()->utf8_length() == jl_object_len &&
329 vmSymbols::java_lang_Object()->starts_with(jl_str, jl_len) &&
330 vmSymbols::java_lang_Object()->ends_with("Object", object_len) &&
331 vmSymbols::java_lang_Object()->is_permanent() &&
332 vmSymbols::java_lang_String()->utf8_length() == jl_object_len &&
333 vmSymbols::java_lang_String()->starts_with(jl_str, jl_len) &&
334 vmSymbols::java_lang_String()->ends_with("String", object_len) &&
335 vmSymbols::java_lang_String()->is_permanent(),
336 "sanity");
337 return true;
338 }
339 #endif //ASSERT
340
341 // returns a symbol; the caller is responsible for decrementing it
find_symbol()342 Symbol* SignatureStream::find_symbol() {
343 // Create a symbol from for string _begin _end
344 int begin = raw_symbol_begin();
345 int end = raw_symbol_end();
346
347 const char* symbol_chars = (const char*)_signature->base() + begin;
348 int len = end - begin;
349
350 // Quick check for common symbols in signatures
351 assert(signature_symbols_sane(), "incorrect signature sanity check");
352 if (len == jl_object_len &&
353 memcmp(symbol_chars, jl_str, jl_len) == 0) {
354 if (memcmp("String", symbol_chars + jl_len, object_len) == 0) {
355 return vmSymbols::java_lang_String();
356 } else if (memcmp("Object", symbol_chars + jl_len, object_len) == 0) {
357 return vmSymbols::java_lang_Object();
358 }
359 }
360
361 Symbol* name = _previous_name;
362 if (name->equals(symbol_chars, len)) {
363 return name;
364 }
365
366 // Save names for cleaning up reference count at the end of
367 // SignatureStream scope.
368 name = SymbolTable::new_symbol(symbol_chars, len);
369
370 // Only allocate the GrowableArray for the _names buffer if more than
371 // one name is being processed in the signature.
372 if (!_previous_name->is_permanent()) {
373 if (_names == NULL) {
374 _names = new GrowableArray<Symbol*>(10);
375 }
376 _names->push(_previous_name);
377 }
378 _previous_name = name;
379 return name;
380 }
381
as_klass(Handle class_loader,Handle protection_domain,FailureMode failure_mode,TRAPS)382 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
383 FailureMode failure_mode, TRAPS) {
384 if (!is_reference()) {
385 return NULL;
386 }
387 Symbol* name = as_symbol();
388 Klass* k = NULL;
389 if (failure_mode == ReturnNull) {
390 // Note: SD::resolve_or_null returns NULL for most failure modes,
391 // but not all. Circularity errors, invalid PDs, etc., throw.
392 k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
393 } else if (failure_mode == CachedOrNull) {
394 NoSafepointVerifier nsv; // no loading, now, we mean it!
395 assert(!HAS_PENDING_EXCEPTION, "");
396 k = SystemDictionary::find(name, class_loader, protection_domain, CHECK_NULL);
397 // SD::find does not trigger loading, so there should be no throws
398 // Still, bad things can happen, so we CHECK_NULL and ask callers
399 // to do likewise.
400 return k;
401 } else {
402 // The only remaining failure mode is NCDFError.
403 // The test here allows for an additional mode CNFException
404 // if callers need to request the reflective error instead.
405 bool throw_error = (failure_mode == NCDFError);
406 k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
407 }
408
409 return k;
410 }
411
as_java_mirror(Handle class_loader,Handle protection_domain,FailureMode failure_mode,TRAPS)412 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
413 FailureMode failure_mode, TRAPS) {
414 if (!is_reference()) {
415 return Universe::java_mirror(type());
416 }
417 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
418 if (klass == NULL) {
419 return NULL;
420 }
421 return klass->java_mirror();
422 }
423
skip_to_return_type()424 void SignatureStream::skip_to_return_type() {
425 while (!at_return_type()) {
426 next();
427 }
428 }
429
ResolvingSignatureStream(Symbol * signature,Handle class_loader,Handle protection_domain,bool is_method)430 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature,
431 Handle class_loader,
432 Handle protection_domain,
433 bool is_method)
434 : SignatureStream(signature, is_method),
435 _class_loader(class_loader), _protection_domain(protection_domain)
436 {
437 initialize_load_origin(NULL);
438 }
439
ResolvingSignatureStream(Symbol * signature,Klass * load_origin,bool is_method)440 ResolvingSignatureStream::ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method)
441 : SignatureStream(signature, is_method)
442 {
443 assert(load_origin != NULL, "");
444 initialize_load_origin(load_origin);
445 }
446
ResolvingSignatureStream(const Method * method)447 ResolvingSignatureStream::ResolvingSignatureStream(const Method* method)
448 : SignatureStream(method->signature(), true)
449 {
450 initialize_load_origin(method->method_holder());
451 }
452
ResolvingSignatureStream(fieldDescriptor & field)453 ResolvingSignatureStream::ResolvingSignatureStream(fieldDescriptor& field)
454 : SignatureStream(field.signature(), false)
455 {
456 initialize_load_origin(field.field_holder());
457 }
458
cache_handles(TRAPS)459 void ResolvingSignatureStream::cache_handles(TRAPS) {
460 assert(_load_origin != NULL, "");
461 _class_loader = Handle(THREAD, _load_origin->class_loader());
462 _protection_domain = Handle(THREAD, _load_origin->protection_domain());
463 }
464
as_klass_if_loaded(TRAPS)465 Klass* ResolvingSignatureStream::as_klass_if_loaded(TRAPS) {
466 Klass* klass = as_klass(CachedOrNull, THREAD);
467 // SD::find does not trigger loading, so there should be no throws
468 // Still, bad things can happen, so we CHECK_NULL and ask callers
469 // to do likewise.
470 if (HAS_PENDING_EXCEPTION) {
471 CLEAR_PENDING_EXCEPTION;
472 }
473 return klass;
474 }
475
476 #ifdef ASSERT
477
478 extern bool signature_constants_sane(); // called from basic_types_init()
479
signature_constants_sane()480 bool signature_constants_sane() {
481 // for the lookup table, test every 8-bit code point, and then some:
482 for (int i = -256; i <= 256; i++) {
483 int btcode = 0;
484 switch (i) {
485 #define EACH_SIG(ch, bt, ignore) \
486 case ch: { btcode = bt; break; }
487 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
488 #undef EACH_SIG
489 }
490 int btc = decode_signature_char(i);
491 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
492 }
493 return true;
494 }
495
is_valid_method_signature(Symbol * sig)496 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
497 const char* method_sig = (const char*)sig->bytes();
498 ssize_t len = sig->utf8_length();
499 ssize_t index = 0;
500 if (method_sig != NULL && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
501 ++index;
502 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
503 ssize_t res = is_valid_type(&method_sig[index], len - index);
504 if (res == -1) {
505 return false;
506 } else {
507 index += res;
508 }
509 }
510 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
511 // check the return type
512 ++index;
513 return (is_valid_type(&method_sig[index], len - index) == (len - index));
514 }
515 }
516 return false;
517 }
518
is_valid_type_signature(Symbol * sig)519 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
520 const char* type_sig = (const char*)sig->bytes();
521 ssize_t len = sig->utf8_length();
522 return (type_sig != NULL && len >= 1 &&
523 (is_valid_type(type_sig, len) == len));
524 }
525
526 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
527 // Returns -1 if it is not, or the index of the next character that is not part
528 // of the type. The type encoding may end before 'limit' and that's ok.
is_valid_type(const char * type,ssize_t limit)529 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
530 ssize_t index = 0;
531
532 // Iterate over any number of array dimensions
533 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
534 if (index >= limit) {
535 return -1;
536 }
537 switch (type[index]) {
538 case JVM_SIGNATURE_BYTE:
539 case JVM_SIGNATURE_CHAR:
540 case JVM_SIGNATURE_FLOAT:
541 case JVM_SIGNATURE_DOUBLE:
542 case JVM_SIGNATURE_INT:
543 case JVM_SIGNATURE_LONG:
544 case JVM_SIGNATURE_SHORT:
545 case JVM_SIGNATURE_BOOLEAN:
546 case JVM_SIGNATURE_VOID:
547 return index + 1;
548 case JVM_SIGNATURE_CLASS:
549 for (index = index + 1; index < limit; ++index) {
550 char c = type[index];
551 switch (c) {
552 case JVM_SIGNATURE_ENDCLASS:
553 return index + 1;
554 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
555 return -1;
556 default: ; // fall through
557 }
558 }
559 // fall through
560 default: ; // fall through
561 }
562 return -1;
563 }
564
565 #endif // ASSERT
566