1 /* 2 * Copyright (c) 2003, 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 "interpreter/interpreter.hpp" 27 #include "memory/gcLocker.hpp" 28 #include "memory/heapInspection.hpp" 29 #include "memory/metadataFactory.hpp" 30 #include "oops/constMethod.hpp" 31 #include "oops/method.hpp" 32 33 // Static initialization 34 const u2 ConstMethod::MAX_IDNUM = 0xFFFE; 35 const u2 ConstMethod::UNSET_IDNUM = 0xFFFF; 36 37 ConstMethod* ConstMethod::allocate(ClassLoaderData* loader_data, 38 int byte_code_size, 39 InlineTableSizes* sizes, 40 MethodType method_type, 41 TRAPS) { 42 int size = ConstMethod::size(byte_code_size, sizes); 43 return new (loader_data, size, true, MetaspaceObj::ConstMethodType, THREAD) ConstMethod( 44 byte_code_size, sizes, method_type, size); 45 } 46 47 ConstMethod::ConstMethod(int byte_code_size, 48 InlineTableSizes* sizes, 49 MethodType method_type, 50 int size) { 51 52 No_Safepoint_Verifier no_safepoint; 53 init_fingerprint(); 54 set_constants(NULL); 55 set_stackmap_data(NULL); 56 set_code_size(byte_code_size); 57 set_constMethod_size(size); 58 set_inlined_tables_length(sizes); // sets _flags 59 set_method_type(method_type); 60 assert(this->size() == size, "wrong size for object"); 61 set_name_index(0); 62 set_signature_index(0); 63 set_constants(NULL); 64 set_max_stack(0); 65 set_max_locals(0); 66 set_method_idnum(0); 67 set_size_of_parameters(0); 68 } 69 70 // Accessor that copies to metadata. 71 void ConstMethod::copy_stackmap_data(ClassLoaderData* loader_data, 72 u1* sd, int length, TRAPS) { 73 _stackmap_data = MetadataFactory::new_array<u1>(loader_data, length, CHECK); 74 memcpy((void*)_stackmap_data->adr_at(0), (void*)sd, length); 75 } 76 77 // Deallocate metadata fields associated with ConstMethod* 78 void ConstMethod::deallocate_contents(ClassLoaderData* loader_data) { 79 if (stackmap_data() != NULL) { 80 MetadataFactory::free_array<u1>(loader_data, stackmap_data()); 81 } 82 set_stackmap_data(NULL); 83 84 // deallocate annotation arrays 85 if (has_method_annotations()) 86 MetadataFactory::free_array<u1>(loader_data, method_annotations()); 87 if (has_parameter_annotations()) 88 MetadataFactory::free_array<u1>(loader_data, parameter_annotations()); 89 if (has_type_annotations()) 90 MetadataFactory::free_array<u1>(loader_data, type_annotations()); 91 if (has_default_annotations()) 92 MetadataFactory::free_array<u1>(loader_data, default_annotations()); 93 } 94 95 // How big must this constMethodObject be? 96 97 int ConstMethod::size(int code_size, 98 InlineTableSizes* sizes) { 99 int extra_bytes = code_size; 100 if (sizes->compressed_linenumber_size() > 0) { 101 extra_bytes += sizes->compressed_linenumber_size(); 102 } 103 if (sizes->checked_exceptions_length() > 0) { 104 extra_bytes += sizeof(u2); 105 extra_bytes += sizes->checked_exceptions_length() * sizeof(CheckedExceptionElement); 106 } 107 if (sizes->localvariable_table_length() > 0) { 108 extra_bytes += sizeof(u2); 109 extra_bytes += 110 sizes->localvariable_table_length() * sizeof(LocalVariableTableElement); 111 } 112 if (sizes->exception_table_length() > 0) { 113 extra_bytes += sizeof(u2); 114 extra_bytes += sizes->exception_table_length() * sizeof(ExceptionTableElement); 115 } 116 if (sizes->generic_signature_index() != 0) { 117 extra_bytes += sizeof(u2); 118 } 119 // This has to be a less-than-or-equal check, because we might be 120 // storing information from a zero-length MethodParameters 121 // attribute. We have to store these, because in some cases, they 122 // cause the reflection API to throw a MalformedParametersException. 123 if (sizes->method_parameters_length() >= 0) { 124 extra_bytes += sizeof(u2); 125 extra_bytes += sizes->method_parameters_length() * sizeof(MethodParametersElement); 126 } 127 128 // Align sizes up to a word. 129 extra_bytes = align_size_up(extra_bytes, BytesPerWord); 130 131 // One pointer per annotation array 132 if (sizes->method_annotations_length() > 0) { 133 extra_bytes += sizeof(AnnotationArray*); 134 } 135 if (sizes->parameter_annotations_length() > 0) { 136 extra_bytes += sizeof(AnnotationArray*); 137 } 138 if (sizes->type_annotations_length() > 0) { 139 extra_bytes += sizeof(AnnotationArray*); 140 } 141 if (sizes->default_annotations_length() > 0) { 142 extra_bytes += sizeof(AnnotationArray*); 143 } 144 145 int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord; 146 assert(extra_words == extra_bytes/BytesPerWord, "should already be aligned"); 147 return align_object_size(header_size() + extra_words); 148 } 149 150 Method* ConstMethod::method() const { 151 return _constants->pool_holder()->method_with_idnum(_method_idnum); 152 } 153 154 // linenumber table - note that length is unknown until decompression, 155 // see class CompressedLineNumberReadStream. 156 157 u_char* ConstMethod::compressed_linenumber_table() const { 158 // Located immediately following the bytecodes. 159 assert(has_linenumber_table(), "called only if table is present"); 160 return code_end(); 161 } 162 163 // Last short in ConstMethod* before annotations 164 u2* ConstMethod::last_u2_element() const { 165 int offset = 0; 166 if (has_method_annotations()) offset++; 167 if (has_parameter_annotations()) offset++; 168 if (has_type_annotations()) offset++; 169 if (has_default_annotations()) offset++; 170 return (u2*)((AnnotationArray**)constMethod_end() - offset) - 1; 171 } 172 173 u2* ConstMethod::generic_signature_index_addr() const { 174 // Located at the end of the constMethod. 175 assert(has_generic_signature(), "called only if generic signature exists"); 176 return last_u2_element(); 177 } 178 179 u2* ConstMethod::method_parameters_length_addr() const { 180 assert(has_method_parameters(), "called only if table is present"); 181 return has_generic_signature() ? (last_u2_element() - 1) : 182 last_u2_element(); 183 } 184 185 u2* ConstMethod::checked_exceptions_length_addr() const { 186 // Located immediately before the generic signature index. 187 assert(has_checked_exceptions(), "called only if table is present"); 188 if(has_method_parameters()) { 189 // If method parameters present, locate immediately before them. 190 return (u2*)method_parameters_start() - 1; 191 } else { 192 // Else, the exception table is at the end of the constMethod. 193 return has_generic_signature() ? (last_u2_element() - 1) : 194 last_u2_element(); 195 } 196 } 197 198 u2* ConstMethod::exception_table_length_addr() const { 199 assert(has_exception_handler(), "called only if table is present"); 200 if (has_checked_exceptions()) { 201 // If checked_exception present, locate immediately before them. 202 return (u2*) checked_exceptions_start() - 1; 203 } else { 204 if(has_method_parameters()) { 205 // If method parameters present, locate immediately before them. 206 return (u2*)method_parameters_start() - 1; 207 } else { 208 // Else, the exception table is at the end of the constMethod. 209 return has_generic_signature() ? (last_u2_element() - 1) : 210 last_u2_element(); 211 } 212 } 213 } 214 215 u2* ConstMethod::localvariable_table_length_addr() const { 216 assert(has_localvariable_table(), "called only if table is present"); 217 if (has_exception_handler()) { 218 // If exception_table present, locate immediately before them. 219 return (u2*) exception_table_start() - 1; 220 } else { 221 if (has_checked_exceptions()) { 222 // If checked_exception present, locate immediately before them. 223 return (u2*) checked_exceptions_start() - 1; 224 } else { 225 if(has_method_parameters()) { 226 // If method parameters present, locate immediately before them. 227 return (u2*)method_parameters_start() - 1; 228 } else { 229 // Else, the exception table is at the end of the constMethod. 230 return has_generic_signature() ? (last_u2_element() - 1) : 231 last_u2_element(); 232 } 233 } 234 } 235 } 236 237 // Update the flags to indicate the presence of these optional fields. 238 void ConstMethod::set_inlined_tables_length(InlineTableSizes* sizes) { 239 _flags = 0; 240 if (sizes->compressed_linenumber_size() > 0) 241 _flags |= _has_linenumber_table; 242 if (sizes->generic_signature_index() != 0) 243 _flags |= _has_generic_signature; 244 if (sizes->method_parameters_length() >= 0) 245 _flags |= _has_method_parameters; 246 if (sizes->checked_exceptions_length() > 0) 247 _flags |= _has_checked_exceptions; 248 if (sizes->exception_table_length() > 0) 249 _flags |= _has_exception_table; 250 if (sizes->localvariable_table_length() > 0) 251 _flags |= _has_localvariable_table; 252 253 // annotations, they are all pointer sized embedded objects so don't have 254 // a length embedded also. 255 if (sizes->method_annotations_length() > 0) 256 _flags |= _has_method_annotations; 257 if (sizes->parameter_annotations_length() > 0) 258 _flags |= _has_parameter_annotations; 259 if (sizes->type_annotations_length() > 0) 260 _flags |= _has_type_annotations; 261 if (sizes->default_annotations_length() > 0) 262 _flags |= _has_default_annotations; 263 264 // This code is extremely brittle and should possibly be revised. 265 // The *_length_addr functions walk backwards through the 266 // constMethod data, using each of the length indexes ahead of them, 267 // as well as the flags variable. Therefore, the indexes must be 268 // initialized in reverse order, or else they will compute the wrong 269 // offsets. Moving the initialization of _flags into a separate 270 // block solves *half* of the problem, but the following part will 271 // still break if the order is not exactly right. 272 // 273 // Also, the servicability agent needs to be informed anytime 274 // anything is added here. It might be advisable to have some sort 275 // of indication of this inline. 276 if (sizes->generic_signature_index() != 0) 277 *(generic_signature_index_addr()) = sizes->generic_signature_index(); 278 // New data should probably go here. 279 if (sizes->method_parameters_length() >= 0) 280 *(method_parameters_length_addr()) = sizes->method_parameters_length(); 281 if (sizes->checked_exceptions_length() > 0) 282 *(checked_exceptions_length_addr()) = sizes->checked_exceptions_length(); 283 if (sizes->exception_table_length() > 0) 284 *(exception_table_length_addr()) = sizes->exception_table_length(); 285 if (sizes->localvariable_table_length() > 0) 286 *(localvariable_table_length_addr()) = sizes->localvariable_table_length(); 287 } 288 289 int ConstMethod::method_parameters_length() const { 290 return has_method_parameters() ? *(method_parameters_length_addr()) : -1; 291 } 292 293 MethodParametersElement* ConstMethod::method_parameters_start() const { 294 u2* addr = method_parameters_length_addr(); 295 u2 length = *addr; 296 assert(length > 0, "should only be called if table is present"); 297 addr -= length * sizeof(MethodParametersElement) / sizeof(u2); 298 return (MethodParametersElement*) addr; 299 } 300 301 302 int ConstMethod::checked_exceptions_length() const { 303 return has_checked_exceptions() ? *(checked_exceptions_length_addr()) : 0; 304 } 305 306 307 CheckedExceptionElement* ConstMethod::checked_exceptions_start() const { 308 u2* addr = checked_exceptions_length_addr(); 309 u2 length = *addr; 310 assert(length > 0, "should only be called if table is present"); 311 addr -= length * sizeof(CheckedExceptionElement) / sizeof(u2); 312 return (CheckedExceptionElement*) addr; 313 } 314 315 316 int ConstMethod::localvariable_table_length() const { 317 return has_localvariable_table() ? *(localvariable_table_length_addr()) : 0; 318 } 319 320 321 LocalVariableTableElement* ConstMethod::localvariable_table_start() const { 322 u2* addr = localvariable_table_length_addr(); 323 u2 length = *addr; 324 assert(length > 0, "should only be called if table is present"); 325 addr -= length * sizeof(LocalVariableTableElement) / sizeof(u2); 326 return (LocalVariableTableElement*) addr; 327 } 328 329 int ConstMethod::exception_table_length() const { 330 return has_exception_handler() ? *(exception_table_length_addr()) : 0; 331 } 332 333 ExceptionTableElement* ConstMethod::exception_table_start() const { 334 u2* addr = exception_table_length_addr(); 335 u2 length = *addr; 336 assert(length > 0, "should only be called if table is present"); 337 addr -= length * sizeof(ExceptionTableElement) / sizeof(u2); 338 return (ExceptionTableElement*)addr; 339 } 340 341 AnnotationArray** ConstMethod::method_annotations_addr() const { 342 assert(has_method_annotations(), "should only be called if method annotations are present"); 343 return (AnnotationArray**)constMethod_end() - 1; 344 } 345 346 AnnotationArray** ConstMethod::parameter_annotations_addr() const { 347 assert(has_parameter_annotations(), "should only be called if method parameter annotations are present"); 348 int offset = 1; 349 if (has_method_annotations()) offset++; 350 return (AnnotationArray**)constMethod_end() - offset; 351 } 352 353 AnnotationArray** ConstMethod::type_annotations_addr() const { 354 assert(has_type_annotations(), "should only be called if method type annotations are present"); 355 int offset = 1; 356 if (has_method_annotations()) offset++; 357 if (has_parameter_annotations()) offset++; 358 return (AnnotationArray**)constMethod_end() - offset; 359 } 360 361 AnnotationArray** ConstMethod::default_annotations_addr() const { 362 assert(has_default_annotations(), "should only be called if method default annotations are present"); 363 int offset = 1; 364 if (has_method_annotations()) offset++; 365 if (has_parameter_annotations()) offset++; 366 if (has_type_annotations()) offset++; 367 return (AnnotationArray**)constMethod_end() - offset; 368 } 369 370 // copy annotations from 'cm' to 'this' 371 void ConstMethod::copy_annotations_from(ConstMethod* cm) { 372 if (cm->has_method_annotations()) { 373 assert(has_method_annotations(), "should be allocated already"); 374 set_method_annotations(cm->method_annotations()); 375 } 376 if (cm->has_parameter_annotations()) { 377 assert(has_parameter_annotations(), "should be allocated already"); 378 set_parameter_annotations(cm->parameter_annotations()); 379 } 380 if (cm->has_type_annotations()) { 381 assert(has_type_annotations(), "should be allocated already"); 382 set_type_annotations(cm->type_annotations()); 383 } 384 if (cm->has_default_annotations()) { 385 assert(has_default_annotations(), "should be allocated already"); 386 set_default_annotations(cm->default_annotations()); 387 } 388 } 389 390 // Printing 391 392 void ConstMethod::print_on(outputStream* st) const { 393 ResourceMark rm; 394 assert(is_constMethod(), "must be constMethod"); 395 st->print_cr("%s", internal_name()); 396 st->print(" - method: " INTPTR_FORMAT " ", p2i((address)method())); 397 method()->print_value_on(st); st->cr(); 398 if (has_stackmap_table()) { 399 st->print(" - stackmap data: "); 400 stackmap_data()->print_value_on(st); 401 st->cr(); 402 } 403 } 404 405 // Short version of printing ConstMethod* - just print the name of the 406 // method it belongs to. 407 void ConstMethod::print_value_on(outputStream* st) const { 408 assert(is_constMethod(), "must be constMethod"); 409 st->print(" const part of method " ); 410 method()->print_value_on(st); 411 } 412 413 #if INCLUDE_SERVICES 414 // Size Statistics 415 void ConstMethod::collect_statistics(KlassSizeStats *sz) const { 416 int n1, n2, n3; 417 sz->_const_method_bytes += (n1 = sz->count(this)); 418 sz->_bytecode_bytes += (n2 = code_size()); 419 sz->_stackmap_bytes += (n3 = sz->count_array(stackmap_data())); 420 421 // Count method annotations 422 int a1 = 0, a2 = 0, a3 = 0, a4 = 0; 423 if (has_method_annotations()) { 424 sz->_methods_annotations_bytes += (a1 = sz->count_array(method_annotations())); 425 } 426 if (has_parameter_annotations()) { 427 sz->_methods_parameter_annotations_bytes += (a2 = sz->count_array(parameter_annotations())); 428 } 429 if (has_type_annotations()) { 430 sz->_methods_type_annotations_bytes += (a3 = sz->count_array(type_annotations())); 431 } 432 if (has_default_annotations()) { 433 sz->_methods_default_annotations_bytes += (a4 = sz->count_array(default_annotations())); 434 } 435 436 int size_annotations = a1 + a2 + a3 + a4; 437 438 sz->_method_all_bytes += n1 + n3 + size_annotations; // note: n2 is part of n3 439 sz->_ro_bytes += n1 + n3 + size_annotations; 440 } 441 #endif // INCLUDE_SERVICES 442 443 // Verification 444 445 void ConstMethod::verify_on(outputStream* st) { 446 guarantee(is_constMethod(), "object must be constMethod"); 447 448 // Verification can occur during oop construction before the method or 449 // other fields have been initialized. 450 guarantee(method()->is_method(), "should be method"); 451 452 address m_end = (address)((intptr_t) this + size()); 453 address compressed_table_start = code_end(); 454 guarantee(compressed_table_start <= m_end, "invalid method layout"); 455 address compressed_table_end = compressed_table_start; 456 // Verify line number table 457 if (has_linenumber_table()) { 458 CompressedLineNumberReadStream stream(compressed_linenumber_table()); 459 while (stream.read_pair()) { 460 guarantee(stream.bci() >= 0 && stream.bci() <= code_size(), "invalid bci in line number table"); 461 } 462 compressed_table_end += stream.position(); 463 } 464 guarantee(compressed_table_end <= m_end, "invalid method layout"); 465 // Verify checked exceptions, exception table and local variable tables 466 if (has_method_parameters()) { 467 u2* addr = method_parameters_length_addr(); 468 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 469 } 470 if (has_checked_exceptions()) { 471 u2* addr = checked_exceptions_length_addr(); 472 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 473 } 474 if (has_exception_handler()) { 475 u2* addr = exception_table_length_addr(); 476 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 477 } 478 if (has_localvariable_table()) { 479 u2* addr = localvariable_table_length_addr(); 480 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 481 } 482 // Check compressed_table_end relative to uncompressed_table_start 483 u2* uncompressed_table_start; 484 if (has_localvariable_table()) { 485 uncompressed_table_start = (u2*) localvariable_table_start(); 486 } else if (has_exception_handler()) { 487 uncompressed_table_start = (u2*) exception_table_start(); 488 } else if (has_checked_exceptions()) { 489 uncompressed_table_start = (u2*) checked_exceptions_start(); 490 } else if (has_method_parameters()) { 491 uncompressed_table_start = (u2*) method_parameters_start(); 492 } else { 493 uncompressed_table_start = (u2*) m_end; 494 } 495 int gap = (intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end; 496 int max_gap = align_object_size(1)*BytesPerWord; 497 guarantee(gap >= 0 && gap < max_gap, "invalid method layout"); 498 }