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