1 /* 2 * Copyright (c) 1997, 2016, 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 "oops/instanceKlass.hpp" 31 #include "oops/oop.inline.hpp" 32 #include "oops/symbol.hpp" 33 #include "oops/typeArrayKlass.hpp" 34 #include "runtime/signature.hpp" 35 36 // Implementation of SignatureIterator 37 38 // Signature syntax: 39 // 40 // Signature = "(" {Parameter} ")" ReturnType. 41 // Parameter = FieldType. 42 // ReturnType = FieldType | "V". 43 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "Q" ValueTypeName ";" | "[" FieldType. 44 // ClassName = string. 45 46 47 SignatureIterator::SignatureIterator(Symbol* signature) { 48 _signature = signature; 49 _parameter_index = 0; 50 } 51 52 void SignatureIterator::expect(char c) { 53 if (_signature->byte_at(_index) != c) fatal("expecting %c", c); 54 _index++; 55 } 56 57 58 void SignatureIterator::skip_optional_size() { 59 Symbol* sig = _signature; 60 char c = sig->byte_at(_index); 61 while ('0' <= c && c <= '9') c = sig->byte_at(++_index); 62 } 63 64 65 int SignatureIterator::parse_type() { 66 // Note: This function could be simplified by using "return T_XXX_size;" 67 // instead of the assignment and the break statements. However, it 68 // seems that the product build for win32_i486 with MS VC++ 6.0 doesn't 69 // work (stack underflow for some tests) - this seems to be a VC++ 6.0 70 // compiler bug (was problem - gri 4/27/2000). 71 int size = -1; 72 switch(_signature->byte_at(_index)) { 73 case 'B': do_byte (); if (_parameter_index < 0 ) _return_type = T_BYTE; 74 _index++; size = T_BYTE_size ; break; 75 case 'C': do_char (); if (_parameter_index < 0 ) _return_type = T_CHAR; 76 _index++; size = T_CHAR_size ; break; 77 case 'D': do_double(); if (_parameter_index < 0 ) _return_type = T_DOUBLE; 78 _index++; size = T_DOUBLE_size ; break; 79 case 'F': do_float (); if (_parameter_index < 0 ) _return_type = T_FLOAT; 80 _index++; size = T_FLOAT_size ; break; 81 case 'I': do_int (); if (_parameter_index < 0 ) _return_type = T_INT; 82 _index++; size = T_INT_size ; break; 83 case 'J': do_long (); if (_parameter_index < 0 ) _return_type = T_LONG; 84 _index++; size = T_LONG_size ; break; 85 case 'S': do_short (); if (_parameter_index < 0 ) _return_type = T_SHORT; 86 _index++; size = T_SHORT_size ; break; 87 case 'Z': do_bool (); if (_parameter_index < 0 ) _return_type = T_BOOLEAN; 88 _index++; size = T_BOOLEAN_size; break; 89 case 'V': do_void (); if (_parameter_index < 0 ) _return_type = T_VOID; 90 _index++; size = T_VOID_size; ; break; 91 case 'L': 92 { int begin = ++_index; 93 Symbol* sig = _signature; 94 while (sig->byte_at(_index++) != ';') ; 95 do_object(begin, _index); 96 } 97 if (_parameter_index < 0 ) _return_type = T_OBJECT; 98 size = T_OBJECT_size; 99 break; 100 case 'Q': 101 { int begin = ++_index; 102 Symbol* sig = _signature; 103 while (sig->byte_at(_index++) != ';') ; 104 do_valuetype(begin, _index); 105 } 106 if (_parameter_index < 0 ) _return_type = T_VALUETYPE; 107 size = T_VALUETYPE_size; 108 break; 109 case '[': 110 { int begin = ++_index; 111 skip_optional_size(); 112 Symbol* sig = _signature; 113 while (sig->byte_at(_index) == '[') { 114 _index++; 115 skip_optional_size(); 116 } 117 if (sig->byte_at(_index) == 'L' || sig->byte_at(_index) == 'Q') { 118 while (sig->byte_at(_index++) != ';') ; 119 } else { 120 _index++; 121 } 122 do_array(begin, _index); 123 if (_parameter_index < 0 ) _return_type = T_ARRAY; 124 } 125 size = T_ARRAY_size; 126 break; 127 default: 128 ShouldNotReachHere(); 129 break; 130 } 131 assert(size >= 0, "size must be set"); 132 return size; 133 } 134 135 136 void SignatureIterator::check_signature_end() { 137 if (_index < _signature->utf8_length()) { 138 tty->print_cr("too many chars in signature"); 139 _signature->print_value_on(tty); 140 tty->print_cr(" @ %d", _index); 141 } 142 } 143 144 145 void SignatureIterator::dispatch_field() { 146 // no '(', just one (field) type 147 _index = 0; 148 _parameter_index = 0; 149 parse_type(); 150 check_signature_end(); 151 } 152 153 154 void SignatureIterator::iterate_parameters() { 155 // Parse parameters 156 _index = 0; 157 _parameter_index = 0; 158 expect('('); 159 while (_signature->byte_at(_index) != ')') _parameter_index += parse_type(); 160 expect(')'); 161 _parameter_index = 0; 162 } 163 164 // Optimized version of iterate_parameters when fingerprint is known 165 void SignatureIterator::iterate_parameters( uint64_t fingerprint ) { 166 uint64_t saved_fingerprint = fingerprint; 167 168 // Check for too many arguments 169 if (fingerprint == (uint64_t)CONST64(-1)) { 170 SignatureIterator::iterate_parameters(); 171 return; 172 } 173 174 assert(fingerprint, "Fingerprint should not be 0"); 175 176 _parameter_index = 0; 177 fingerprint = fingerprint >> (static_feature_size + result_feature_size); 178 while ( 1 ) { 179 switch ( fingerprint & parameter_feature_mask ) { 180 case bool_parm: 181 do_bool(); 182 _parameter_index += T_BOOLEAN_size; 183 break; 184 case byte_parm: 185 do_byte(); 186 _parameter_index += T_BYTE_size; 187 break; 188 case char_parm: 189 do_char(); 190 _parameter_index += T_CHAR_size; 191 break; 192 case short_parm: 193 do_short(); 194 _parameter_index += T_SHORT_size; 195 break; 196 case int_parm: 197 do_int(); 198 _parameter_index += T_INT_size; 199 break; 200 case obj_parm: 201 do_object(0, 0); 202 _parameter_index += T_OBJECT_size; 203 break; 204 case valuetype_parm: 205 do_valuetype(0,0); 206 _parameter_index += T_VALUETYPE_size; 207 break; 208 case long_parm: 209 do_long(); 210 _parameter_index += T_LONG_size; 211 break; 212 case float_parm: 213 do_float(); 214 _parameter_index += T_FLOAT_size; 215 break; 216 case double_parm: 217 do_double(); 218 _parameter_index += T_DOUBLE_size; 219 break; 220 case done_parm: 221 return; 222 break; 223 default: 224 tty->print_cr("*** parameter is " UINT64_FORMAT, fingerprint & parameter_feature_mask); 225 tty->print_cr("*** fingerprint is " PTR64_FORMAT, saved_fingerprint); 226 ShouldNotReachHere(); 227 break; 228 } 229 fingerprint >>= parameter_feature_size; 230 } 231 _parameter_index = 0; 232 } 233 234 235 void SignatureIterator::iterate_returntype() { 236 // Ignore parameters 237 _index = 0; 238 expect('('); 239 Symbol* sig = _signature; 240 // Need to skip over each type in the signature's argument list until a 241 // closing ')' is found., then get the return type. We cannot just scan 242 // for the first ')' because ')' is a legal character in a type name. 243 while (sig->byte_at(_index) != ')') { 244 switch(sig->byte_at(_index)) { 245 case 'B': 246 case 'C': 247 case 'D': 248 case 'F': 249 case 'I': 250 case 'J': 251 case 'S': 252 case 'Z': 253 case 'V': 254 { 255 _index++; 256 } 257 break; 258 case 'Q': 259 case 'L': 260 { 261 while (sig->byte_at(_index++) != ';') ; 262 } 263 break; 264 case '[': 265 { 266 int begin = ++_index; 267 skip_optional_size(); 268 while (sig->byte_at(_index) == '[') { 269 _index++; 270 skip_optional_size(); 271 } 272 if (sig->byte_at(_index) == 'L' || sig->byte_at(_index) == 'Q') { 273 while (sig->byte_at(_index++) != ';') ; 274 } else { 275 _index++; 276 } 277 } 278 break; 279 default: 280 ShouldNotReachHere(); 281 break; 282 } 283 } 284 expect(')'); 285 // Parse return type 286 _parameter_index = -1; 287 parse_type(); 288 check_signature_end(); 289 _parameter_index = 0; 290 } 291 292 293 void SignatureIterator::iterate() { 294 // Parse parameters 295 _parameter_index = 0; 296 _index = 0; 297 expect('('); 298 while (_signature->byte_at(_index) != ')') _parameter_index += parse_type(); 299 expect(')'); 300 // Parse return type 301 _parameter_index = -1; 302 parse_type(); 303 check_signature_end(); 304 _parameter_index = 0; 305 } 306 307 308 // Implementation of SignatureStream 309 SignatureStream::SignatureStream(Symbol* signature, bool is_method) : 310 _signature(signature), _at_return_type(false) { 311 _begin = _end = (is_method ? 1 : 0); // skip first '(' in method signatures 312 _names = new GrowableArray<Symbol*>(10); 313 next(); 314 } 315 316 SignatureStream::~SignatureStream() { 317 // decrement refcount for names created during signature parsing 318 for (int i = 0; i < _names->length(); i++) { 319 _names->at(i)->decrement_refcount(); 320 } 321 } 322 323 bool SignatureStream::is_done() const { 324 return _end > _signature->utf8_length(); 325 } 326 327 328 void SignatureStream::next_non_primitive(int t) { 329 switch (t) { 330 case 'L': { 331 _type = T_OBJECT; 332 Symbol* sig = _signature; 333 while (sig->byte_at(_end++) != ';'); 334 break; 335 } 336 case 'Q': { 337 _type = T_VALUETYPE; 338 Symbol* sig = _signature; 339 while (sig->byte_at(_end++) != ';'); 340 break; 341 } 342 case '[': { 343 _type = T_ARRAY; 344 Symbol* sig = _signature; 345 char c = sig->byte_at(_end); 346 while ('0' <= c && c <= '9') c = sig->byte_at(_end++); 347 while (sig->byte_at(_end) == '[') { 348 _end++; 349 c = sig->byte_at(_end); 350 while ('0' <= c && c <= '9') c = sig->byte_at(_end++); 351 } 352 switch(sig->byte_at(_end)) { 353 case 'B': 354 case 'C': 355 case 'D': 356 case 'F': 357 case 'I': 358 case 'J': 359 case 'S': 360 case 'Z':_end++; break; 361 default: { 362 while (sig->byte_at(_end++) != ';'); 363 break; 364 } 365 } 366 break; 367 } 368 case ')': _end++; next(); _at_return_type = true; break; 369 default : ShouldNotReachHere(); 370 } 371 } 372 373 374 bool SignatureStream::is_object() const { 375 return _type == T_OBJECT 376 || _type == T_ARRAY 377 || _type == T_VALUETYPE; 378 } 379 380 bool SignatureStream::is_array() const { 381 return _type == T_ARRAY; 382 } 383 384 Symbol* SignatureStream::as_symbol(TRAPS) { 385 // Create a symbol from for string _begin _end 386 int begin = _begin; 387 int end = _end; 388 389 if ( ( _signature->byte_at(_begin) == 'L' || _signature->byte_at(_begin) == 'Q' ) 390 && _signature->byte_at(_end-1) == ';') { 391 begin++; 392 end--; 393 } 394 395 // Save names for cleaning up reference count at the end of 396 // SignatureStream scope. 397 Symbol* name = SymbolTable::new_symbol(_signature, begin, end, CHECK_NULL); 398 _names->push(name); // save new symbol for decrementing later 399 return name; 400 } 401 402 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain, 403 FailureMode failure_mode, TRAPS) { 404 if (!is_object()) return NULL; 405 Symbol* name = as_symbol(CHECK_NULL); 406 if (failure_mode == ReturnNull) { 407 return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD); 408 } else { 409 bool throw_error = (failure_mode == NCDFError); 410 return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD); 411 } 412 } 413 414 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain, 415 FailureMode failure_mode, TRAPS) { 416 if (!is_object()) 417 return Universe::java_mirror(type()); 418 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL); 419 if (klass == NULL) return NULL; 420 return klass->java_mirror(); 421 } 422 423 Symbol* SignatureStream::as_symbol_or_null() { 424 // Create a symbol from for string _begin _end 425 ResourceMark rm; 426 427 int begin = _begin; 428 int end = _end; 429 430 if ( ( _signature->byte_at(_begin) == 'L' || _signature->byte_at(_begin) == 'Q' ) 431 && _signature->byte_at(_end-1) == ';') { 432 begin++; 433 end--; 434 } 435 436 char* buffer = NEW_RESOURCE_ARRAY(char, end - begin); 437 for (int index = begin; index < end; index++) { 438 buffer[index - begin] = _signature->byte_at(index); 439 } 440 Symbol* result = SymbolTable::probe(buffer, end - begin); 441 return result; 442 } 443 444 int SignatureStream::reference_parameter_count() { 445 int args_count = 0; 446 for ( ; !at_return_type(); next()) { 447 if (is_object()) { 448 args_count++; 449 } 450 } 451 return args_count; 452 } 453 454 bool SignatureVerifier::is_valid_signature(Symbol* sig) { 455 const char* signature = (const char*)sig->bytes(); 456 ssize_t len = sig->utf8_length(); 457 if (signature == NULL || signature[0] == '\0' || len < 1) { 458 return false; 459 } else if (signature[0] == '(') { 460 return is_valid_method_signature(sig); 461 } else { 462 return is_valid_type_signature(sig); 463 } 464 } 465 466 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) { 467 const char* method_sig = (const char*)sig->bytes(); 468 ssize_t len = sig->utf8_length(); 469 ssize_t index = 0; 470 if (method_sig != NULL && len > 1 && method_sig[index] == '(') { 471 ++index; 472 while (index < len && method_sig[index] != ')') { 473 ssize_t res = is_valid_type(&method_sig[index], len - index); 474 if (res == -1) { 475 return false; 476 } else { 477 index += res; 478 } 479 } 480 if (index < len && method_sig[index] == ')') { 481 // check the return type 482 ++index; 483 return (is_valid_type(&method_sig[index], len - index) == (len - index)); 484 } 485 } 486 return false; 487 } 488 489 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) { 490 const char* type_sig = (const char*)sig->bytes(); 491 ssize_t len = sig->utf8_length(); 492 return (type_sig != NULL && len >= 1 && 493 (is_valid_type(type_sig, len) == len)); 494 } 495 496 // Checks to see if the type (not to go beyond 'limit') refers to a valid type. 497 // Returns -1 if it is not, or the index of the next character that is not part 498 // of the type. The type encoding may end before 'limit' and that's ok. 499 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) { 500 ssize_t index = 0; 501 502 // Iterate over any number of array dimensions 503 while (index < limit && type[index] == '[') ++index; 504 if (index >= limit) { 505 return -1; 506 } 507 switch (type[index]) { 508 case 'B': case 'C': case 'D': case 'F': case 'I': 509 case 'J': case 'S': case 'Z': case 'V': 510 return index + 1; 511 case 'Q': 512 case 'L': 513 for (index = index + 1; index < limit; ++index) { 514 char c = type[index]; 515 if (c == ';') { 516 return index + 1; 517 } 518 if (invalid_name_char(c)) { 519 return -1; 520 } 521 } 522 // fall through 523 default: ; // fall through 524 } 525 return -1; 526 } 527 528 bool SignatureVerifier::invalid_name_char(char c) { 529 switch (c) { 530 case '\0': case '.': case ';': case '[': 531 return true; 532 default: 533 return false; 534 } 535 }