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